How to display a monochrome color from a true color image?

I have trouble displaying only the Red, Green and Blue components of my edge detection algorithm I'm using. The edges are detected for my true color RGB image, but I need to display three images representing the RED, GREEN and BLUE edge detections respectively.
I can do the edge detection for gray scale images, but the RGB one's are troublesome. Could some one please help me with this? I'm attaching my code below.
Kind regards, Louis
clear all; close all; clc;
[filename path] = uigetfile('*.*');
m=imread(filename);
figure,image(m); % Display the original picture
r=m(:,:,1); %extract the red color
g=m(:,:,2); %extract the green color
b=m(:,:,3); %extract the blue
hx =[ -1 -2 -1; 0 0 0; 1 2 1];
hy =[ -1 0 1; -2 0 2; -1 0 1];
% x dirction 'sobel' processing
xr=filter2(hx,r);
xg=filter2(hx,g);
xb=filter2(hx,b);
%combine the three color
t1(:,:,1)=xr;
t1(:,:,2)=xg;
t1(:,:,3)=xb;
size(t1)
t=uint8(t1);
figure, image(t);
% y dirction 'sobel' processing
yr=filter2(hy,r);
yg=filter2(hy,g);
yb=filter2(hy,b);
%combine the three color
t2(:,:,1)=yr;
t2(:,:,2)=yg;
t2(:,:,3)=yb;
t=uint8(t2);
figure, image(t);
%combine x and y direction processing
t3(:,:,1)=xr+yr;
t3(:,:,2)=xg+yg;
t3(:,:,3)=xb+yb;
t = uint8(t3);
figure, image(t);

 Accepted Answer

I thought we had this all figure out a few days ago. Didn't I give you the demo code to show r, g and b images, and Walter gave you code to cast them into the appropriate color channel to make a rue color out of the individual color channels to make them show up in "their" color? To display, you use image(), or imagesc(), or imshow(). imshow() is my preferred function. Let me know if you want to see it again.
Anyway, doing a Sobel filter on the separate color channels will give an edge image with color artifacts. You should do it on the value channel of an hsv image if you want to avoid color artifacts.

7 Comments

Yep, that is what I remember as well.
Thanks. I know we solved this color issue a few days ago, but when I go with that approach, I get some errors regarding those masked colors not wanting to convolve in the filter2() function. I got this code somewhere on the internet and there was a portion on the HSV image (using rgb2hsv() ), but the result was the same as for the code above. The edges still have colors "mixed" in them. They are not "pure" red, green and blue edges.
Who's to say what the "true" edge is? If you want, just take any one of them or some combination of them. Unless you have some ground truth, then just pick something and continue on with whatever else you're going to do. If you do have ground truth, then pick the one that's best. What is your ultimate goal anyway, such that you think you need color edge detection filters as a way to solve it?
My goal is to obtain and display the three monochrome edges of any RGB image first separately, and then sum these three color edged to finally obtain the RGB image's edges in red, green and blue.
I know that it's difficult to claim whether or not the edge is a "true" edge. I've finished my parts on the DFT, low-pass filtering and the IDFT [all of which was self-coded and NO Matlab functions were to be used that delivers the same result e.g. fft( ) ]. I am able to display the red, green and blue masked images as well as a gray one, but now all that remains is to detect the edges on each of these three color images as well as the final result for the edges on the original color image.
Your assignment (below) is sufficiently vague that it gives you lots of latitude in how you can define "merge." What I'd probably do is to take the sqrt of the sum of the squares of your x- and y- Sobel filter images.
sobelXY = sqrt(double(xr).^2 + double(yr) .^2 and so on...
Then threshold them at some value (say 4 or 5) to get a binary image - a "map" or where the "strong" edges are:
binaryImage = sobelXY > 4;
Adjust that number to take stronger or weaker edges. Call this thresholded image "binaryImage" or "edgeMap" or whatever. Then burn those edge pixels into the image, say in red, by doing this
outputR = r;
outputG = g;
outputB = b;
outputR(binaryImage) = 255;
outputG(binaryImage) = 0;
outputB(binaryImage) = 0;
outputRGB = cat(3, outputR, outputG, outputB);
imshow(outputRGB);
At least that's one way of doing it.
Thanks Image Analyst, I will implement this and see if I get something better than what I currently have.
I notice when I tune that threshold value in binaryImage from 4 to about 200 in random rough intervals, the displayed image's red edges are strong and finer, but the original image is still present. I suppose we have to display ONLY the edges and not the original image still in the background. This already help me a lot! Now i just need to display only the edges of the image... like the original snippet I posted gives.

Sign in to comment.

More Answers (2)

There are the two assignments I should complete. I can say with confidence, I've finished Assignment 1 and it works relatively good, with only a limitation on processing speed for very large images due to the DFT properties. It is the last two bullets of the second assignment I'm having problems with.
Assignment 1:
Display a black and white image on the computer screen;
Determine and display the discrete time Fourier Transform of the image;
Remove the high frequency components of the image by filtering;
Calculate the inverse discrete time Fourier Transform of the filtered image and display it alongside the original image in order to illustrate the effect of the low-pass filter.
Assignment 2:
Display a color image on the computer screen;
Display the RGB components (red, green and blue) of the image;
Determine and display the edges of the red, green and blue components found in the original image – edge detection;
Merge the red, green and blue component images wherein the edges have been detected, into one image and display the edges that are found in the original color image.
Thanks yet again for all the help, tips and guidance!

1 Comment

See my answer in this: http://www.mathworks.com/matlabcentral/answers/29527-criteria-of-a-good-image-testing

Sign in to comment.

Final report on this topic: I have managed to solve this problem, as stated above -- meeting the requirements for my assignment. I want to thank you Image Analyst, for all your guidance and support. This helped me to achieve my goal, by providing some tools and a basic understanding I could apply to my problem.
I also want the thank Walter Roberson for this contribution regarding the concatenation of the color planes. This "technique" I learned helped a lot in this assignment. See http://www.mathworks.com/matlabcentral/answers/38448-how-do-i-get-a-colormap-for-my-image for other portions of the final solution (tools that helped me).
I hope that my struggles and solutions may help someone else in the future who bump into the same blocks I did. Optimization to this code is always accepted, unfortunately time was not on my side.
clear all; close all; clc; echo off;
[filename filepath] = uigetfile('*.*');
tic
[X, map] = imread(filename);
info = imfinfo(filename)
width = getfield(imfinfo(filename),'Width');
height = getfield(imfinfo(filename),'Height');
G = rgb2gray(X);
% Grayscale edge detection
buffer = zeros(size(G,1)+2 , size(G,2)+2);
bdim = size(buffer);
buffer(2:bdim(1)-1 , 2:bdim(2)-1) = G(:,:);
C = double(buffer);
B = G;
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=( (C(i+2,j) + 2*C(i+2,j+1) + C(i+2,j+2) ) - ...
( C(i,j) + 2*C(i,j+1) + C(i,j+2) ) );
%Sobel mask for y-direction:
Gy=( (C(i,j+2) + 2*C(i+1,j+2) + C(i+2,j+2) ) - ...
( C(i,j) + 2*C(i+1,j) + C(i+2,j) ) );
%Edge detected pixel values:
B(i,j)= ceil( sqrt(Gx.^2 + Gy.^2) );
W(i,j)= ceil( 255 - B(i,j) );
%Check WoB image
if W(i,j) > 255
W(i,j) = 255;
end
if W(i,j) < 0
W(i,j) = 0;
end
%Check BoW image
if B(i,j) > 255
B(i,j) = 255;
end
if B(i,j) < 0
B(i,j) = 0;
end
end
end
figure,imshow(G); title('Original image');
figure,imshow(B); title('Edges in White on Black');
figure,imshow(W); title('Edges in Black on White');
toc
clear all;

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!