Prev: jumping to output directly
Next: need help on imread
From: Digistar You on 12 Apr 2010 16:09 "Image Analyst" <imageanalyst(a)mailinator.com> wrote in message <hpqf95$r55$1(a)fred.mathworks.com>... > OK for some reason I took an interest in it, and went to some trouble for you to create a demo image myself (from the standard coin demo image). See if this demo makes sense to you: > Make sure you join any lines split into two by the newsreader! > > clc; % Clear the command window. > close all; % Close all figures (except those of imtool.) > clear all; % Erase all existing variables. > workspace; % Make sure the workspace panel is showing. > fontSize = 18; > > % Read in standard MATLAB grayscale demo image. > grayImage = imread('eight.tif'); > subplot(3, 3, 1); > imshow(grayImage, []); > title('Original Grayscale Image'); > set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. > > binaryImage = imfill(grayImage < 200, 'holes'); > binaryImage = bwareaopen(binaryImage, 1000); > subplot(3, 3, 2); > imshow(binaryImage, []); > title('Binary Image'); > > adjacentImage = bwmorph(binaryImage, 'thick', inf); > subplot(3, 3, 3); > imshow(adjacentImage, []); > title('Adjacent Image'); > > [labeledImage numberOfBlobs] = bwlabel(adjacentImage, 8); % Label each blob so we can make measurements of it > subplot(3, 3, 4); > imshow(labeledImage, []); > title('Labeled Image'); > > % Label the blobs. > blobMeasurements = regionprops(labeledImage, 'centroid'); > for k = 1 : numberOfBlobs % Loop through all blobs. > blobCentroid = blobMeasurements(k).Centroid; % Get centroid. > text(blobCentroid(1), blobCentroid(2), num2str(k), ... > 'Color', [0 0 1], 'FontSize', fontSize); > end > > coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels > subplot(3, 3, 5); > imshow(coloredLabels, []); > title('Colored Labeled Image'); > > adjacentImageL = imdilate(labeledImage, ones(3)); > subplot(3, 3, 6); > imshow(adjacentImageL, []); > title('Adjacent Labeled Image'); > > % Finally, we now have the image that we can use graycomatrix on. > kernel = [0 1; 0 -1; 1 0; -1 0]; > glcm = graycomatrix(adjacentImageL, 'NumLevels', numberOfBlobs, 'GrayLimits', [],'offset',kernel); > for k = 1:size(glcm,3); > glcm(:,:,k) % Display in the command window. > end > > % Combine all directions into one. > glcm = sum(glcm, 3); > subplot(3, 3, 7); > imshow(glcm, []); > title('GLCM'); > > % Go through saying which blob is next to which other blob. > for row = 1:numberOfBlobs > for col = row+1:numberOfBlobs > if glcm(row, col) > 0 > promptMessage = sprintf('Blobs #%d and #%d are next to each other.', row, col); > else > promptMessage = sprintf('Blobs #%d and #%d are NOT next to each other.', row, col); > end > button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue'); > if strcmp(button, 'Cancel') > return; > end > end > end Dear friend, Thank you so much for your help, I really appreciate. With the help of your program, I finally found where my problem comes from. In my program, an image was segmented into different regions and then labels, e.g. label values from 1-30. However, after that, I did a region merging. For example, 30 regions might be merged into 20 regions, and I still used the original labels values, e.g. 1, 4, 5, 7, ..., 30. In this case, graycomatrix cannot give a correct answer. But if I used a continuous label values for the merged region, i.e. 1,2,3,4,...20. graycomatrix can give what I want. So that is the problem. Now my program runs well and very fast. Thank you very much.
From: ImageAnalyst on 12 Apr 2010 16:14
You're welcome. I'm glad it's working now. |