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

39 views (last 30 days)
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

Image Analyst
Image Analyst on 20 May 2012
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
Louis Kok
Louis Kok on 20 May 2012
Thanks Image Analyst, I will implement this and see if I get something better than what I currently have.
Louis Kok
Louis Kok on 20 May 2012
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)

Louis Kok
Louis Kok on 20 May 2012
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
Image Analyst
Image Analyst on 20 May 2012
See my answer in this: http://www.mathworks.com/matlabcentral/answers/29527-criteria-of-a-good-image-testing

Sign in to comment.


Louis Kok
Louis Kok on 20 May 2012
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;

Community Treasure Hunt

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

Start Hunting!