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 view (last 30 days)
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.

Answers (2)

Turlough Hughes
Turlough Hughes on 28 Feb 2020
Edited: Turlough Hughes on 28 Feb 2020
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 Comments
Vinitha Patil
Vinitha Patil on 1 Mar 2020
Is it possible to find area of objects in each divided block of image. If area is in between 10 to 200 pixel then the object is weed mark the position or centroide of the weed on original plant image. Does it really work with blockproc() function. please help me out.
Turlough Hughes
Turlough Hughes on 1 Mar 2020
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.

Sign in to comment.


Matt J
Matt J on 1 Mar 2020
Using sepblockfun from the File Exchange
Areas=sepblockfun(B,[40,50],'sum');

Categories

Find more on Agriculture in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!