applying same function to different image areas
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
giacomo
am 26 Okt. 2017
Kommentiert: Adam
am 27 Okt. 2017
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 :)
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 26 Okt. 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 Kommentare
Adam
am 27 Okt. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Feature Detection and Extraction finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!