matlab code How to dived 400*500 image into 40*50 and calculate object area in each block. put all blocks in one imafe
1 回表示 (過去 30 日間)
古いコメントを表示
I am trying to classify weed and crop. I did feature extraction.finally I got Binary image of the crop and weed.The last part isclassification based on area of weed and crop.
I am doing classification based on pixel area of binary image. For that I want divide wholw image into 40*50 of 100 blocks. Find the area in each block.If pixel B(i,j)=1, count= cont +1. If white pixel count in the block more 80% that is crop otherwise it is weed. For that I want process all blocks and pur result in one image.I have problem with looping plese help me. to do all.
0 件のコメント
回答 (2 件)
Turlough Hughes
2020 年 2 月 28 日
編集済み: Turlough Hughes
2020 年 2 月 28 日
You can find out which blocks are crops (according to your definitions) as follows:
B = blockproc(Img, [40 50], @(block_struct)...
(nnz(block_struct.data)/numel(block_struct.data))>0.8 )
B is just a 10x10 array corresponding to your blocks.
By wanting the results in one output image do you mean you want the blocks less than or equal to 80% to appear black? Try the following script.
clear all
close all
clc
fontSize = 16;
% Generate image
Img = false(400,500);
ind = randperm(numel(Img),numel(Img)*0.8);
Img(ind) = true;
figure('units','normalized','outerposition',[0 0 1 1])
subplot(1,2,1);
imshow(Img)
title('Input image (approx. 80% white pixels).','fontSize',fontSize);
% Block process sets blocks which consist of <= 80% true pixels to all 0.
B = blockproc(Img, [40 50], @(block_struct) myfun(block_struct.data));
subplot(1,2,2);
imshow(B)
title('Block processing result','fontSize',fontSize)
function blockout = myfun(blockin)
if nnz(blockin)/numel(blockin) > 0.8
blockout = blockin;
else
blockout = false(size(blockin));
end
end
7 件のコメント
Turlough Hughes
2020 年 3 月 1 日
If area is in between 10 to 200 pixel then the object is weed.
I tried and that doesn't distinuish your crops from your weeds, only one 40 by 50 block passes that condition. If you want to try different blockprocessing strategies then modify the local function at the bottom of the script I provided. If you have any question about how the above code works I am happy to explain further.
Matt J
2020 年 3 月 1 日
Using sepblockfun from the File Exchange
Areas=sepblockfun(B,[40,50],'sum');
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!