- У меня есть набор данных изображения RGB с соответствующими значениями глубины в текстовых файлах.
- Я делаю цветовую сегментацию deltaE в одной части
- А также делаю сегментацию глубины в другой части .
- После получения двоичного изображения глубинной сегментации я пытаюсь отобразить в него значения RGB исходного изображения, используя координаты пикселя.
- Я закодировал это далеко и Вывод выглядит как ниже, где мы видим, что позиция пикселя смещена. Я не могу понять, где я ошибся.
clc
clear all
close all
cd Image
fontSize = 14;
dir_Ik=dir
pathI=pwd;
cd ..
cd Depth
dir_Dk =dir
pathD=pwd;
cd ..
%create folder
mkdir DepthHandDetection
cd DepthHandDetection
output=dir
pathO=pwd;
cd ..
mkdir ColorHandDetection
cd ColorHandDetection
Coutput=dir
CpathO=pwd;
cd ..
% all folder inside Image and Depth(P1 to P10)
for i= 3: length(dir_Ik)
dir_If=strcat((strcat(pathI,'\')),dir_Ik(i).name);
cd (dir_If)
PI=dir;
cd ..
dir_Df=strcat((strcat(pathD,'\')),dir_Dk(i).name);
cd (dir_Df)
PD=dir;
cd ..
dir_Op=strcat((strcat(pathO,'\')),dir_Dk(i).name);
newfolder = fullfile(sprintf('%s%d', dir_Op));
mkdir(newfolder);
Cdir_Op=strcat((strcat(CpathO,'\')),dir_Ik(i).name);
Cnewfolder = fullfile(sprintf('%s%d', Cdir_Op));
mkdir(Cnewfolder);
for j=3:length(PI)
% all folder inside Image and Depth (G1 to G10)
f1=strcat((strcat(dir_If,'\')),PI(j).name);
cd (f1)
Limg=dir;
d1=strcat((strcat(dir_Df,'\')),PD(j).name);
cd (d1)
LDg=dir;
o1=strcat((strcat(dir_Op,'\')),PD(j).name);
newfolder1 = fullfile(sprintf('%s%d', o1));
mkdir(newfolder1);
Co1=strcat((strcat(Cdir_Op,'\')),PI(j).name);
Cnewfolder1 = fullfile(sprintf('%s%d', Co1));
mkdir(Cnewfolder1);
% r=100;
% cnt=0;
% all files inside Image and Depth (jpg and txt)
for a=3:length(Limg)
if (j==3 & a==3)
f=strcat((strcat(f1,'\')),Limg(a).name);
[rgbImage, storedColorMap] = imread(f);
[rows, columns, numberOfColorBands] = size(rgbImage);
t=strcat((strcat(d1,'\')),LDg(a).name);
D=load(t);
% If it's monochrome (indexed), convert it to color.
% Check to see if it's an 8-bit image needed later for scaling).
if strcmpi(class(rgbImage), 'uint8')
% Flag for 256 gray levels.
eightBit = true;
else
eightBit = false;
end
if numberOfColorBands == 1
if isempty(storedColorMap)
% Just a simple gray level image, not indexed with a stored color map.
% Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
else
% It's an indexed image.
rgbImage = ind2rgb(rgbImage, storedColorMap);
% ind2rgb() will convert it to double and normalize it to the range 0-1.
% Convert back to uint8 in the range 0-255, if needed.
if eightBit
rgbImage = uint8(255 * rgbImage);
end
end
end
% Display the original image.
set(gcf, 'Position', get(0, 'ScreenSize'));
set(gcf,'name','Color Segmentation','numbertitle','off')
h1 = subplot(2,3,1);
imshow(rgbImage);pause(0.001);
drawnow; % Make it display immediately.
normalizedImage = uint8(255*mat2gray(D));
thresh=normalizedImage;
img=255*ones(480,640,'uint8');
img(thresh>=21 & thresh<=29)=0;
img=imtranslate(img,[40, 8],'FillValues',255);
% img(thresh==0)=0;
img=img+rgbImage;
subplot(2,3,2);
imshow(img); pause(0.001);
if numberOfColorBands > 1
title('Original Color Image', 'FontSize', fontSize);
else
caption = sprintf('Original Indexed Image\n(converted to true color with its stored colormap)');
title(caption, 'FontSize', fontSize);
end
mask = DrawFreehandRegion(h1, rgbImage); % Draw a freehand, irregularly-shaped region.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, class(rgbImage)));
cform = makecform('srgb2lab');
lab_Image = applycform(im2double(rgbImage),cform);
% Extract out the color bands from the original image
% into 3 separate 2D arrays, one for each color component.
LChannel = lab_Image(:, :, 1);
aChannel = lab_Image(:, :, 2);
bChannel = lab_Image(:, :, 3);
% Get the average lab color value.
[LMean, aMean, bMean] = GetMeanLABValues(LChannel, aChannel, bChannel, mask);
% Make uniform images of only that one single LAB color.
LStandard = LMean * ones(rows, columns);
aStandard = aMean * ones(rows, columns);
bStandard = bMean * ones(rows, columns);
% Create the delta images: delta L, delta A, and delta B.
deltaL = LChannel - LStandard;
deltaa = aChannel - aStandard;
deltab = bChannel - bStandard;
% Create the Delta E image.
% This is an image that represents the color difference.
% Delta E is the square root of the sum of the squares of the delta images.
deltaE = sqrt(deltaL .^ 2 + deltaa .^ 2 + deltab .^ 2);
% Mask it to get the Delta E in the mask region only.
maskedDeltaE = deltaE .* mask;
% Get the mean delta E in the mask region
% Note: deltaE(mask) is a 1D vector of ONLY the pixel values within the masked area.
meanMaskedDeltaE = mean(deltaE(mask));
% Get the standard deviation of the delta E in the mask region
stDevMaskedDeltaE = std(deltaE(mask));
message = sprintf('The mean LAB = (%.2f, %.2f, %.2f).\nThe mean Delta E in the masked region is %.2f +/- %.2f',...
LMean, aMean, bMean, meanMaskedDeltaE, stDevMaskedDeltaE);
numberOfLines = 1;
% Set the default tolerance to be the mean delta E in the masked region plus two standard deviations.
strTolerance = sprintf('%.1f', meanMaskedDeltaE + 3 * stDevMaskedDeltaE);
defaultAnswer = {strTolerance}; % Suggest this number to the user.
prompt = {sprintf('First, examine the histogram.\nThen find pixels within this Delta E (from the average color in the region you drew):')};
dialogTitle = 'Enter Delta E Tolerance';
% numberOfLines = 1;
response = inputdlg(prompt, dialogTitle, numberOfLines, defaultAnswer);
% Update tolerance with user's response.
tolerance = str2double(cell2mat(response));
% Find pixels within that delta E.
binaryImage = deltaE <= tolerance;
subplot(2,3,3);
imshow(binaryImage, []);
pause(0.001);
title('Matching', 'FontSize', fontSize);
end
%%
f=strcat((strcat(f1,'\')),Limg(a).name)
x=imread(f);
t=strcat((strcat(d1,'\')),LDg(a).name);
D=load(t);
normalizedImage = uint8(255*mat2gray(D));
thresh=normalizedImage;
img=255*ones(480,640,'uint8');
img(thresh>=21 & thresh<=29)=0;
img=imtranslate(img,[40, 8],'FillValues',255);
% img(thresh==0)=0;
img=img+rgbImage;
subplot(2,3,2);
imshow(img); pause(0.001);
[rgbImage, storedColorMap] = imread(f);
[rows, columns, numberOfColorBands] = size(rgbImage);
% If it's monochrome (indexed), convert it to color.
% Check to see if it's an 8-bit image needed later for scaling).
if strcmpi(class(rgbImage), 'uint8')
% Flag for 256 gray levels.
eightBit = true;
else
eightBit = false;
end
if numberOfColorBands == 1
if isempty(storedColorMap)
% Just a simple gray level image, not indexed with a stored color map.
% Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
else
% It's an indexed image.
rgbImage = ind2rgb(rgbImage, storedColorMap);
% ind2rgb() will convert it to double and normalize it to the range 0-1.
% Convert back to uint8 in the range 0-255, if needed.
if eightBit
rgbImage = uint8(255 * rgbImage);
end
end
end
subplot(2,3,1);
imshow(rgbImage);pause(0.001);
rgbImage1=imresize(rgbImage,size(mask));
maskedRgbImage = bsxfun(@times, rgbImage1, cast(mask, class(rgbImage)));
cform = makecform('srgb2lab');
lab_Image = applycform(im2double(rgbImage1),cform);
% Extract out the color bands from the original image
% into 3 separate 2D arrays, one for each color component.
LChannel = lab_Image(:, :, 1);
aChannel = lab_Image(:, :, 2);
bChannel = lab_Image(:, :, 3);
% % Get the average lab color value.
[LMean, aMean, bMean] = GetMeanLABValues(LChannel, aChannel, bChannel, mask);
% Make uniform images of only that one single LAB color.
LStandard = LMean * ones(rows, columns);
aStandard = aMean * ones(rows, columns);
bStandard = bMean * ones(rows, columns);
LStandard1=imresize(LStandard,size(LChannel));
aStandard1=imresize(aStandard,size(aChannel));
bStandard1=imresize(bStandard,size(bChannel));
% Create the delta images: delta L, delta A, and delta B.
deltaL = LChannel - LStandard1;
deltaa = aChannel - aStandard1;
deltab = bChannel - bStandard1;
% Create the Delta E image.
% This is an image that represents the color difference.
% Delta E is the square root of the sum of the squares of the delta images.
deltaE = sqrt(deltaL .^ 2 + deltaa .^ 2 + deltab .^ 2);
% imshow(deltaE, []);
deltaE1=imresize(deltaE,size(mask));
% Mask it to get the Delta E in the mask region only.
maskedDeltaE = deltaE1 .* mask;
% Get the mean delta E in the mask region
% Note: deltaE(mask) is a 1D vector of ONLY the pixel values within the masked area.
meanMaskedDeltaE = mean(deltaE(mask));
% Get the standard deviation of the delta E in the mask region
stDevMaskedDeltaE = std(deltaE(mask));
message = sprintf('The mean LAB = (%.2f, %.2f, %.2f).\nThe mean Delta E in the masked region is %.2f +/- %.2f',...
LMean, aMean, bMean, meanMaskedDeltaE, stDevMaskedDeltaE);
tolerance = strTolerance;
% Find pixels within that delta E.
binaryImage = deltaE <= str2double(tolerance);
subplot(2,3,3);
imshow(binaryImage, []);
pause(0.001);
title('Matching', 'FontSize', fontSize);
rgbImage1=imresize(rgbImage,size(binaryImage));
% Mask the image with the matching colors and extract those pixels.
matchingColors = bsxfun(@times, rgbImage1, cast(binaryImage, class(rgbImage)));
matchingColors=matchingColors+img;
subplot(2,3,4);
% matchingColors=im2bw(matchingColors);
% a=strel('disk',2);
% matchingColors=imclose(matchingColors,a);
imshow(matchingColors);
pause(0.001);
% caption = sprintf('MatchingOrigDelta E <= %.1f)', tolerance);
title('MatchingOrig', 'FontSize', fontSize);
% Mask the image with the NON-matching colors and extract those pixels.
nonMatchingColors = bsxfun(@times, rgbImage1, cast(~binaryImage, class(rgbImage)));
% fullFileName = strcat((strcat(pathO,'\')),Limg(i).name);
o2=strcat((strcat(o1,'\')),Limg(a).name);
newfolder2 = fullfile(sprintf('%s%d', o2));
imwrite(img, newfolder2);
Co2=strcat((strcat(Co1,'\')),Limg(a).name);
Cnewfolder2 = fullfile(sprintf('%s%d', Co2));
imwrite(matchingColors, Cnewfolder2);
end
cd ..
end
cd ..
end
cd ..
% end
%-----------------------------------------------------------------------------
function [xCoords, yCoords, roiPosition] = DrawBoxRegion(handleToImage)
enlargeForDrawing = true;
axes(handleToImage);
if enlargeForDrawing
hImage = findobj(gca,'Type','image');
numberOfImagesInside = length(hImage);
if numberOfImagesInside > 1
imageInside = get(hImage(1), 'CData');
else
imageInside = get(hImage, 'CData');
end
hTemp = figure;
hImage2 = imshow(imageInside, []);
[rows columns NumberOfColorBands] = size(imageInside);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
end
txtInfo = sprintf('Draw a box over the unstained fabric by clicking and dragging over the image.\nDouble click inside the box to finish drawing.');
text(10, 40, txtInfo, 'color', 'r', 'FontSize', 24);
% Prompt user to draw a region on the image.
msgboxw(txtInfo);
% Erase all previous lines.
if ~enlargeForDrawing
axes(handleToImage);
% ClearLinesFromAxes(handles);
end
hBox = imrect;
roiPosition = wait(hBox);
roiPosition
% Erase all previous lines.
if ~enlargeForDrawing
axes(handleToImage);
% ClearLinesFromAxes(handles);
end
xCoords = [roiPosition(1), roiPosition(1)+roiPosition(3), roiPosition(1)+roiPosition(3), roiPosition(1), roiPosition(1)];
yCoords = [roiPosition(2), roiPosition(2), roiPosition(2)+roiPosition(4), roiPosition(2)+roiPosition(4), roiPosition(2)];
% Plot the mask as an outline over the image.
hold on;
plot(xCoords, yCoords, 'linewidth', 2);
close(hTemp);
return; % from DrawRegion
end
%-----------------------------------------------------------------------------
function [mask] = DrawFreehandRegion(handleToImage, rgbImage)
fontSize = 14;
% Open a temporary full-screen figure if requested.
enlargeForDrawing = true;
axes(handleToImage);
if enlargeForDrawing
hImage = findobj(gca,'Type','image');
numberOfImagesInside = length(hImage);
if numberOfImagesInside > 1
imageInside = get(hImage(1), 'CData');
else
imageInside = get(hImage, 'CData');
end
hTemp = figure;
hImage2 = imshow(imageInside, []);
[rows columns NumberOfColorBands] = size(imageInside);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
end
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
text(10, 40, message, 'color', 'r', 'FontSize', fontSize);
% Prompt user to draw a region on the image.
uiwait(msgbox(message));
hFH = imfreehand();
mask = hFH.createMask();
close(hTemp);
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
return; % from DrawFreehandRegion
end
%-----------------------------------------------------------------------------
% Get the average lab within the mask region.
function [LMean, aMean, bMean] = GetMeanLABValues(LChannel, aChannel, bChannel, mask)
LVector = LChannel(mask); % 1D vector of only the pixels within the masked area.
LMean = mean(LVector);
aVector = aChannel(mask); % 1D vector of only the pixels within the masked area.
aMean = mean(aVector);
bVector = bChannel(mask); % 1D vector of only the pixels within the masked area.
bMean = mean(bVector);
return; % from GetMeanLABValues
end
%==========================================================================================================================
function WarnUser(warningMessage)
uiwait(warndlg(warningMessage));
return; % from WarnUser()
end
%==========================================================================================================================
function msgboxw(message)
uiwait(msgbox(message));
return; % from msgboxw()
end
%==========================================================================================================================
% Plots the histograms of the pixels in both the masked region and the entire image.
function PlotHistogram(maskedRegion, doubleImage, plotNumber, caption)
fontSize = 14;
subplot(plotNumber(1), plotNumber(2), plotNumber(3));
% Find out where the edges of the histogram bins should be.
maxValue1 = max(maskedRegion(:));
maxValue2 = max(doubleImage(:));
maxOverallValue = max([maxValue1 maxValue2]);
edges = linspace(0, maxOverallValue, 100);
% Get the histogram of the masked region into 100 bins.
pixelCount1 = histc(maskedRegion(:), edges);
% Get the histogram of the entire image into 100 bins.
pixelCount2 = histc(doubleImage(:), edges);
% Plot the histogram of the entire image.
plot(edges, pixelCount2, 'b-');
% Now plot the histogram of the masked region.
% However there will likely be so few pixels that this plot will be so low and flat compared to the histogram of the entire
% image that you probably won't be able to see it. To get around this, let's scale it to make it higher so we can see it.
gainFactor = 1.0;
maxValue3 = max(max(pixelCount2));
pixelCount3 = gainFactor * maxValue3 * pixelCount1 / max(pixelCount1);
hold on;
plot(edges, pixelCount3, 'r-');
title(caption, 'FontSize', fontSize);
% Scale x axis manually.
xlim([0 edges(end)]);
legend('Entire', 'Masked');
return; % from PlotHistogram
end
%=====================================================================
% Shows vertical lines going up from the X axis to the curve on the plot.
function lineHandle = PlaceVerticalBarOnPlot(handleToPlot, x, lineColor)
% If the plot is visible, plot the line.
if get(handleToPlot, 'visible')
axes(handleToPlot); % makes existing axes handles.axesPlot the current axes.
% Make sure x location is in the valid range along the horizontal X axis.
XRange = get(handleToPlot, 'XLim');
maxXValue = XRange(2);
if x > maxXValue
x = maxXValue;
end
hold on;
yLimits = ylim;
lineHandle = line([x x], [yLimits(1) yLimits(2)], 'Color', lineColor, 'LineWidth', 3);
hold off;
end
return; % End of PlaceVerticalBarOnPlot
end
Мой набор данных связан следующим образом: https://drive.google.com/file/d/1f8tUHid1KmnwbgskGMXmobOxMfbxIgHM/view Пример комбинированного ввода-вывода выглядит так: Цветное изображение и выходное изображение рядом