applying same function to different image areas

1 view (last 30 days)
Hi everyone. I uploaded an image, cropped it and divided it into 4 stripes like this:
>> I = imread(fullImageFileName);
[Crop, rect] = imcrop(I);
[r, c, p] = size(Crop);
Crop(1:r/4:r,:,:)=255;
A = Crop(1:r/4,:,:);
B = Crop(r/4+1:r/2,:,:);
C = Crop(r/2+1:3*r/4,:,:);
D = Crop(3*r/4+1:r,:,:);
imshow(Crop);
And I have a function from SimpleColorDetectionByHue() that is defined here:
>> function [meanHSV, areas, numberOfBlobs] = MeasureBlobs(maskImage, hImage, sImage, vImage)
try
[labeledImage, numberOfBlobs] = bwlabel(maskImage, 8); % Label each blob so we can make measurements of it
if numberOfBlobs == 0
% Didn't detect any blobs of the specified color in this image.
meanHSV = [0 0 0];
areas = 0;
return;
end
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurementsHue = regionprops(labeledImage, hImage, 'area', 'MeanIntensity');
blobMeasurementsSat = regionprops(labeledImage, sImage, 'area', 'MeanIntensity');
blobMeasurementsValue = regionprops(labeledImage, vImage, 'area', 'MeanIntensity');
meanHSV = zeros(numberOfBlobs, 3); % One row for each blob. One column for each color.
meanHSV(:,1) = [blobMeasurementsHue.MeanIntensity]';
meanHSV(:,2) = [blobMeasurementsSat.MeanIntensity]';
meanHSV(:,3) = [blobMeasurementsValue.MeanIntensity]';
% Now assign the areas.
areas = zeros(numberOfBlobs, 3); % One row for each blob. One column for each color.
areas(:,1) = [blobMeasurementsHue.Area]';
areas(:,2) = [blobMeasurementsSat.Area]';
areas(:,3) = [blobMeasurementsValue.Area]';
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from MeasureBlobs()
And recalled in the main as here:
>> % Measure the mean HSV and area of all the detected blobs.
[meanHSV, areas, numberOfBlobs] = MeasureBlobs(coloredObjectsMask, hImage, sImage, vImage);
if numberOfBlobs > 0
fprintf(1, '\n----------------------------------------------\n');
fprintf(1, 'Blob #, Area in Pixels, Mean H, Mean S, Mean V\n');
fprintf(1, '----------------------------------------------\n');
for blobNumber = 1 : numberOfBlobs
fprintf(1, '#%5d, %14d, %6.2f, %6.2f, %6.2f\n', blobNumber, areas(blobNumber), ...
meanHSV(blobNumber, 1), meanHSV(blobNumber, 2), meanHSV(blobNumber, 3));
end
I want to repeat this function for the 4 regions of my image and display the 'regional' results, then maybe group the 'total' results in a matrix using a sum. How can I do it? Thanks a lot :)

Accepted Answer

Image Analyst
Image Analyst on 26 Oct 2017
Just call it 4 times
[meanHSVA, areasA, numberOfBlobsA] = MeasureBlobs(maskImageA, hImageA, sImageA, vImageA)
[meanHSVB, areasB, numberOfBlobsB] = MeasureBlobs(maskImageB, hImageB, sImageB, vImageB)
[meanHSVC, areasC, numberOfBlobsC] = MeasureBlobs(maskImageC, hImageC, sImageC, vImageC)
[meanHSVD, areasD, numberOfBlobsD] = MeasureBlobs(maskImageD, hImageD, sImageD, vImageD)
  2 Comments
giacomo
giacomo on 27 Oct 2017
Only modifying that part? Where else should I add the letter suffix? I tried to modify the main creating 4 different if loops right after recalling the function but it doesn't work. I also defined all of the variables before calling the 4 functions like this:
>> coloredObjectsMaskA = coloredObjectsMask(1:rows/4,:,:);
coloredObjectsMaskB = coloredObjectsMask(rows/4+1:rows/2,:,:);
coloredObjectsMaskC = coloredObjectsMask(rows/2+1:3*rows/4,:,:);
coloredObjectsMaskD = coloredObjectsMask(3*rows/4+1:rows,:,:);
hImageA = hImage(1:rows/4,:,:);
hImageB = hImage(rows/4+1:rows/2,:,:);
hImageC = hImage(rows/2+1:3*rows/4,:,:);
hImageD = hImage(3*rows/4+1:rows,:,:);
sImageA = sImage(1:rows/4,:,:);
sImageB = sImage(rows/4+1:rows/2,:,:);
sImageC = sImage(rows/2+1:3*rows/4,:,:);
sImageD = sImage(3*rows/4+1:rows,:,:);
vImageA = vImage(1:rows/4,:,:);
vImageB = vImage(rows/4+1:rows/2,:,:);
vImageC = vImage(rows/2+1:3*rows/4,:,:);
vImageD = vImage(3*rows/4+1:rows,:,:);
but it doesn't seem to help. Sorry I know that is basic knowledge but I never done this before.
Adam
Adam on 27 Oct 2017
Saying 'it doesn't work' and 'doesn't seem to help' is not much use. Tell us exactly what doesn't work. Do you get an error? Do you get incorrect results? etc etc.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!