I have several images and I am wondering which methods I can use to segment from the background.

1 件のコメント

Rik
Rik 2017 年 6 月 29 日
I think I would binarize this image, making all white-ish pixels true. Then you can label the areas to select the lobster.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2017 年 6 月 30 日

3 投票

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 = 'L4.jpg';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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;
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image by thresholding.
mask = grayImage < 125;
% Display the mask image.
subplot(2, 2, 3);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Binary Image Mask', 'fontSize', fontSize);
drawnow;
% Get rid of blobs touching the border.
mask = imclearborder(mask);
% Extract just the largest blob.
mask = bwareafilt(mask, 1);
% Display the mask image.
subplot(2, 2, 4);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Lobster-only Mask', 'FontSize', fontSize);
drawnow;
% Get rid of black islands (holes) in struts without filling large black areas.
subplot(2, 2, 4);
mask = ~bwareaopen(~mask, 1000);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Final Cleaned Mask', 'FontSize', fontSize);
drawnow;

6 件のコメント

Image Analyst
Image Analyst 2017 年 7 月 1 日
Try graythresh() and im2bw() instead of thresholding using a value of 125.
Of course it would be best if you could take a shot without any lobster there and divide the lobster image by the blank image to undo the non-uniform lighting and exposure.
Abdallah Abdallah
Abdallah Abdallah 2017 年 7 月 1 日
OK. Thank you very much, Sir. But I still don't understand how to find the head and tail (the distance between the head and tail) of the lobster. I would appreciate if you can show me that please.
Best Regards
Image Analyst
Image Analyst 2017 年 7 月 1 日
I don't know how to do it automatically - it would take a very long time to totally automate that with all the different configurations of shape and whether the claw pixels "connect" with the head pixels. Therefore I suggest you use imdistline() or improfile() to do computer-assisted user-drawn lines.
Abdallah Abdallah
Abdallah Abdallah 2017 年 7 月 2 日
Ok. Sorry to bother you much on this. How about getting the point from tip of the tail to other end of the lobster (which I suppose could be the tip of the claw or somewhere around the head). With that, I would be able to have approximate value (distance) because as you mentioned, the are several configurations around the head and it is even more cluttered in some of the pictures I have. I would appreciate any help or sample of how I could do this Sir.
Regards
Image Analyst
Image Analyst 2017 年 7 月 2 日
It's kind of kludgy but I'd use the radon transform to find the tail. it will be the thinnest part. Radon demo attached. Then once I know where the tail is, I'd chop of off and call regionprops to get the "orientation" angle. I'd also find the centroid of the tail and draw a line at that angle through the tail centroid. Get this line and call bwareafilt(bwLine, 1) to extract the longest contiguous line. That will be the head to tail distance. I don't have any code for that specialized algorithm, so give it a try yourself.
Abdallah Abdallah
Abdallah Abdallah 2017 年 7 月 5 日
Ok. thanks a lot. I will try the algorithm.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGeometric Transformation and Image Registration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by