Info

This question is closed. Reopen it to edit or answer.

Hey !! I downloaded a code that segments part of an image . The object that i want to segment is a plant leaf but after applying segmentation a black image appears . I need someone to help me to find whats wrong with the following code

1 view (last 30 days)
Hey !! I downloaded a code that segments part of an image . The object that i want to segment is a plant leaf but after applying segmentation a black image appears . I need someone to help me to find whats wrong with the following code
function seghsv()
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
set(gcf, 'Position', get(0, 'ScreenSize'));
% Convert RGB image to HSV
rgbImage =imread('m.jpg');
hsvImage = rgb2hsv(rgbImage);
% Extract out the H, S, and V images individually
hImage = hsvImage(:,:,1);
sImage = hsvImage(:,:,2);
vImage = hsvImage(:,:,3);
% Display them.
subplot(3, 4, 1);
imshow(hsvImage);
title('Hsv Image');
subplot(3, 4, 2);
imshow(hImage);
title('Hue Image');
subplot(3, 4, 3);
imshow(sImage);
title('Saturation Image');
subplot(3, 4, 4);
imshow(vImage);
title('Value Image');
% Compute and plot the histogram of the "hue" band.
hHuePlot = subplot(3, 4, 6);
[hueCounts, hueBinValues] = imhist(hImage);
maxHueBinValue = find(hueCounts > 0, 1, 'last');
maxCountHue = max(hueCounts);
bar(hueBinValues, hueCounts, 'r');
grid on;
xlabel('Hue Value');
ylabel('Pixel Count');
title('Histogram of Hue Image');
% Compute and plot the histogram of the "saturation" band.
hSaturationPlot = subplot(3, 4, 7);
[saturationCounts, saturationBinValues] = imhist(sImage);
maxSaturationBinValue = find(saturationCounts > 0, 1, 'last');
maxCountSaturation = max(saturationCounts);
bar(saturationBinValues, saturationCounts, 'g', 'BarWidth', 0.95);
grid on;
xlabel('Saturation Value');
ylabel('Pixel Count');
title('Histogram of Saturation Image');
% Compute and plot the histogram of the "value" band.
hValuePlot = subplot(3, 4, 8);
[valueCounts, valueBinValues] = imhist(vImage);
maxValueBinValue = find(valueCounts > 0, 1, 'last');
maxCountValue = max(valueCounts);
bar(valueBinValues, valueCounts, 'b');
grid on;
xlabel('Value Value');
ylabel('Pixel Count');
title('Histogram of Value Image');
% Set all axes to be the same width and height.
% This makes it easier to compare them.
maxCount = max([maxCountHue, maxCountSaturation, maxCountValue]);
axis([hHuePlot hSaturationPlot hValuePlot], [0 1 0 maxCount]);
% Plot all 3 histograms in one plot.
subplot(3, 4, 5);
plot(hueBinValues, hueCounts, 'r', 'LineWidth', 2);
grid on;
xlabel('Values');
ylabel('Pixel Count');
hold on;
plot(saturationBinValues, saturationCounts, 'g', 'LineWidth', 2);
plot(valueBinValues, valueCounts, 'b', 'LineWidth', 2);
title('Histogram of All Bands');
maxGrayLevel = max([maxHueBinValue, maxSaturationBinValue, maxValueBinValue]);
% Make x-axis to just the max gray level on the bright end.
xlim([0 1]);
% Take a guess at the values that might work for the user's image.
hueThresholdLow = 0.34;
hueThresholdHigh = 0.98;
saturationThresholdLow = 0.4;
saturationThresholdHigh = 1;
valueThresholdLow = 0.8;
valueThresholdHigh = 1.0;
PlaceThresholdBars(6, hueThresholdLow, hueThresholdHigh);
PlaceThresholdBars(7, saturationThresholdLow, saturationThresholdHigh);
PlaceThresholdBars(8, valueThresholdLow, valueThresholdHigh);
hueMask = (hImage >= hueThresholdLow) & (hImage <= hueThresholdHigh);
saturationMask = (sImage >= saturationThresholdLow) & (sImage <= saturationThresholdHigh);
valueMask = (vImage >= valueThresholdLow) & (vImage <= valueThresholdHigh);
fontSize = 16;
subplot(3, 4, 10);
imshow(hueMask, []);
title('= Hue Mask', 'FontSize', fontSize);
subplot(3, 4, 11);
imshow(saturationMask, []);
title('& Saturation Mask', 'FontSize', fontSize);
subplot(3, 4, 12);
imshow(valueMask, []);
title('& Value Mask', 'FontSize', fontSize);
% Combine the masks to find where all 3 are "true."
% Then we will have the mask of only the red parts of the image.
yellowObjectsMask = uint8(hueMask & saturationMask & valueMask);
subplot(3, 4, 9);
imshow(yellowObjectsMask, []);
caption = sprintf('Mask of Only\nThe Yellow Objects');
title(caption, 'FontSize', fontSize);
smallestAcceptableArea = 100; % Keep areas only if they're bigger than this.
% Open up a new figure, since the existing one is full.
figure;
% Maximize the figure.
set(gcf, 'Position', get(0, 'ScreenSize'));
% Get rid of small objects. Note: bwareaopen returns a logical.
yellowObjectsMask = uint8(bwareaopen(yellowObjectsMask, smallestAcceptableArea));
subplot(3, 3, 1);
imshow(yellowObjectsMask, []);
fontSize = 13;
caption = sprintf('bwareaopen() removed objects\nsmaller than %d pixels', smallestAcceptableArea);
title(caption, 'FontSize', fontSize);
% Smooth the border using a morphological closing operation, imclose().
structuringElement = strel('disk', 4);
yellowObjectsMask = imclose(yellowObjectsMask, structuringElement);
subplot(3, 3, 2);
imshow(yellowObjectsMask, []);
fontSize = 16;
title('Border smoothed', 'FontSize', fontSize);
% Fill in any holes in the regions, since they are most likely red also.
yellowObjectsMask = uint8(imfill(yellowObjectsMask, 'holes'));
subplot(3, 3, 3);
imshow(yellowObjectsMask, []);
title('Regions Filled', 'FontSize', fontSize);
return;
% Function to show the low and high threshold bars on the histogram plots.
function PlaceThresholdBars(plotNumber, lowThresh, highThresh)
% Show the thresholds as vertical red bars on the histograms.
subplot(3, 4, plotNumber);
hold on;
maxYValue = ylim;
maxXValue = xlim;
hStemLines = stem([lowThresh highThresh], [maxYValue(2) maxYValue(2)], 'r');
children = get(hStemLines, 'children');
set(children(2),'visible', 'off');
% Place a text label on the bar chart showing the threshold.
fontSizeThresh = 14;
annotationTextL = sprintf('%d', lowThresh);
annotationTextH = sprintf('%d', highThresh);
% For text(), the x and y need to be of the data class "double" so let's cast both to double.
text(double(lowThresh + 5), double(0.85 * maxYValue(2)), annotationTextL, 'FontSize', fontSizeThresh, 'Color', [0 .5 0], 'FontWeight', 'Bold');
text(double(highThresh + 5), double(0.85 * maxYValue(2)), annotationTextH, 'FontSize', fontSizeThresh, 'Color', [0 .5 0], 'FontWeight', 'Bold');
% Show the range as arrows.
% Can't get it to work, with either gca or gcf.
% annotation(gca, 'arrow', [lowThresh/maxXValue(2) highThresh/maxXValue(2)],[0.7 0.7]);
return; % from PlaceThresholdBars()
  3 Comments
Walter Roberson
Walter Roberson on 19 Feb 2013
At the moment we have no evidence that the code is wrong. You might be using it incorrectly, or it might be giving the correct answer based on other assumptions. Have you considered the benefit of making a copy of your image available to us so we can see what it is doing in your situation, rather than us trying to read through and understand the code well enough to find all the possible mistakes?
Hint: Image Analyst never bothers with questions like this unless the image is made available.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!