applying same function to different image areas
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Image Analyst
2017년 10월 26일
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
Adam
2017년 10월 27일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Filtering and Enhancement에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!