how to get the threshold automatically when converting image from hsv to bw
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi! I have to convert my image from HSV to BW to do all the analysis. But I have a lot of images and I can't adjust the threshold one by one. I have checked some of my images, and the threshold change from 0.1 to 0.4. SO the change is quite large and I can't use a single threshold for all the image.
So does somebody know how can I generate the threshold for each image without manually checking the histogram? I need to choose the threshold after the first pick in the histgram.
I asked a question related to this one before. More info: https://stackoverflow.com/questions/47024527/how-to-segment-the-colony-when-i-cant-use-bw-image
Some of my codes are here:
b=imread('test4.jpg')
hsvIm = rgb2hsv(b) % convert the image to hsv format
imshow(hsvIm)
histogram(hsvIm(:,:,1))
%thresholding on the hue space
bwIm = hsvIm(:,:,1) < 0.3 % the threshold can change from 0.1 to 0.4
I also attach some pictures here:



Thank you in advance!!!
댓글 수: 11
Guillaume
2017년 11월 9일
편집: Guillaume
2017년 11월 9일
Walter point is that if you want to segment according to a red hue you need to consider hue values near 0 and hue values near 1 (360 degrees) since red is at both ends.
I would add to that that segmenting just using the hue is risky, you also ought to look at the saturation and value. Compare these three images:
figure;
subplot(1, 3, 1); imshow(hsv2rgb(repmat(cat(3, 0.2, 1, 1), 100, 100)));
subplot(1, 3, 2); imshow(hsv2rgb(repmat(cat(3, 0.2, 0.01, 0.01), 100, 100)));
subplot(1, 3, 3); imshow(hsv2rgb(repmat(cat(3, 0.2, 0.01, 0.99), 100, 100)))
They all have the same hue (0.2 == yellowish), yet only one of them look yellow. The other two have very low saturation (0.01) and vastly different values (0.01 and 0.99)
I would actually follow Akira's advice to use k-means clustering in the L*a*b* colour space.
채택된 답변
추가 답변 (2개)
Image Analyst
2017년 11월 7일
Since they seem to all be circular, have you tried imfindcircles()?
댓글 수: 5
Image Analyst
2017년 11월 9일
Exactly what regions are you trying to find? The whole bullseye, or just the center, or something else?
Image Analyst
2017년 11월 14일
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'lab_try.jpg';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%===============================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Compute HSV image.
hsvImage = rgb2hsv(rgbImage);
saturationImage = hsvImage(:, :, 2);
valueImage = hsvImage(:, :, 3);
% Display the image.
subplot(2, 2, 2);
imshow(saturationImage, []);
axis on;
caption = sprintf('Saturation Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Create the binary image.
binaryImage = saturationImage > 0.22 & valueImage > 0.1;
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Take largest blobs only
binaryImage = bwareaopen(binaryImage, 20);
% Fill them to get rid of noise.
binaryImage = imfill(binaryImage, 'holes');
% Display the mask image.
subplot(2, 2, 4);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
props = regionprops(binaryImage, 'Area');
allAreas = sort([props.Area], 'descend')
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


