Can I use grayscale image in Lab color space

조회 수: 7 (최근 30일)
S.M.
S.M. 2018년 8월 29일
댓글: S.M. 2018년 9월 5일
Hello, i tried one image processing tutorial, which i found on YouTube. Basically it's about segmentation of salami pieces on a pizza. For that i have to convert an RGB-image into the CIELAB color space and define different classes. This code works perfectly fine.
Now my question is: Can I use this script to segment things from a gray-scale image? Or do I have to convert the grayscale image into the RGB format?
Here the code:
% Load picture
img = imread('Pizza.jpg');
% Show original pic
subplot(4,3,1);
imshow(img);
% Convert into lab
labimg = rgb2lab(img);
% Adjust and show color channels
subplot(4,3,2);
imshow(labimg(:,:,1),[]);
subplot(4,3,3);
imshow(labimg(:,:,2),[]);
subplot(4,3,4);
imshow(labimg(:,:,3),[]);
% Define classes
salami = labimg(508:603, 200:305, :);
tomato = labimg(634:729, 326:457, :);
plate = labimg(645:708, 615:699, :);
backgr = labimg(3:56, 5:53, :);
% Save averages from classes in a matrix
classes = [[mean2(salami(:,:,2)), mean2(salami(:,:,3))]', [mean2(tomato(:,:,2)), mean2(tomato(:,:,3))]', [mean2(plate(:,:,2)), mean2(plate(:,:,3))]', [mean2(backgr(:,:,2)), mean2(backgr(:,:,3))]'];
[height, width, channels] = size(labimg);
classimg = zeros(height,width);
for i = 1:height
for j = 1:width
classimg(i,j) = nearestNeighbour2d(labimg(i,j,2:3), classes);
end
end
% Show class-image
subplot(4,3,5);
imshow(classimg, []);
% Extraction of salami
salamiclass = (classimg ==1);
salamiclass = double(salamiclass);
subplot(4,3,6);
imshow(salamiclass, []);
% Use erode
se = strel('disk',28);
erodedBW = imerode(salamiclass, se);
subplot(4,3,7);
imshow(erodedBW, []);
% Extraction of the regioons
labels = bwlabel(erodedBW);
subplot(4,3,8);
imshow(labels, []);
numLabels = max(max(labels));
regions = zeros(5,numLabels);
for ii = 1:numLabels
[xx, yy] = find(labels==ii);
regions(1:5,ii) = [min(yy) min(xx) max(yy) max(xx) size(xx,1)]';
end
circles = zeros(numLabels,3);
for ii=1:numLabels
circles(ii,1:3) = [regions(3,ii)-(regions(3,ii)-regions(1,ii))/2 regions(4,ii)-(regions(4,ii)-regions(2,ii))/2 max([regions(3,ii)-regions(1,ii) regions(4,ii)-regions(2,ii)])];
end
img = draw('Circles', img, circles, [0 0 255], 5);
subplot(4,3,9);
imshow(img, []);

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 5일
In theory it would be possible to calculate the L component directly knowing the grayscale value and making assumptions about the whitepoint . The a and b components would both be 0 for true grayscale. You would use that code to replace the labimg = rgb2lab(img); call.
In practice, it is far easier just to code
labimg = rgb2lab( repmat(img, [1 1 3]) );
  댓글 수: 1
S.M.
S.M. 2018년 9월 5일
Thank you Walter, I'm much further now. I'll try it with another picture. Let's see what happens. Thanks again!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by