Problem in plotting the matrix over an image.

3 Ansichten (letzte 30 Tage)
Prachi Sharma
Prachi Sharma am 16 Nov. 2016
Beantwortet: Image Analyst am 17 Nov. 2016
I am required to read an image,find out its rgb channels and then in those channels separately,mark the pixels whose value exceeds above a certain threshold value.I wrote the following code for marking the pixels but they are not getting marked and I am getting the error that it exceeds matrix dimensions.What am I doing wrong in the code? Below is the code-
img=imread('test.jpg');
imshow(img)
redchannel=img(:,:,1);
greenchannel=img(:,:,2);
bluechannel=img(:,:,3);
rs=zeros(size(redchannel));
r1=cat(3,redchannel,rs,rs);
figure
imshow(r1)
gs=zeros(size(greenchannel));
g1=cat(3,gs,greenchannel,gs);
figure
imshow(g1)
bs=zeros(size(bluechannel));
b1=cat(3,bs,bs,bluechannel);
figure
imshow(b1)
[r,c,~]=size(img);
indx=zeros(r,c);
for i=1:r
for j=1:c
if ((redchannel(i,j))>=180)
indx(i,j)=1;
end
end
end
for i=1:r
for j=1:c
imshow(r1)
hold on
plot(indx(:,1),indx(:,2),'b*');
hold off
end
end
  5 Kommentare
Image Analyst
Image Analyst am 16 Nov. 2016
Do you want to change the actual pixel values, like make them red or something? OR do you want to leave the pixel values the same and just change how they are displayed, like use a colormap where those pixels are red, or use a binary overlay ?
Prachi Sharma
Prachi Sharma am 17 Nov. 2016
I need to take an image,find its 3 channels and then in every channel I need to check if any pixel is exceeding a certain threshold value.If a pixel exceeds a certain threshold value then those pixels should be marked or highlighted in the image of that channel.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

KSSV
KSSV am 16 Nov. 2016
clc; clear all ;
img=imread('Prachi Sharma image');
imshow(img)
%
redchannel=img(:,:,1);
greenchannel=img(:,:,2);
bluechannel=img(:,:,3);
%
rs=zeros(size(redchannel));
r1=cat(3,redchannel,rs,rs);
figure
imshow(r1)
%
gs=zeros(size(greenchannel));
g1=cat(3,gs,greenchannel,gs);
figure
imshow(g1)
%
bs=zeros(size(bluechannel));
b1=cat(3,bs,bs,bluechannel);
figure
imshow(b1)
%
[r,c,~]=size(img);
indx=NaN(r,c);
indx(redchannel>=180) = 1 ;
%
imshow(r1) ;
x = 1:c ;
y = 1:r ;
[X,Y] = meshgrid(x,y) ;
hold on
plot3(X,Y,indx,'*b') ;
  3 Kommentare
KSSV
KSSV am 16 Nov. 2016
You try it out...this is the way the matrix size of x,y making equal to indx. Do consider the Guillaume comments.
Prachi Sharma
Prachi Sharma am 16 Nov. 2016
I tried it out,switching places of r and c and it didn't work that way.

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 16 Nov. 2016
Bearbeitet: Guillaume am 16 Nov. 2016
find will return the locations of all the values above the threshold, there's no need for a loop
img = imread('test.jpg');
redchannel = img(:, :, 1);
[row, column] = find(redchannel > 180); %no need for loop
redimage = img; redimage(:, :, [2 3]) = 0;
figure;
imshow(redimage);
hold on;
plot(column, row, 'b*'); %column, row <=> x, y. Stupid matlab can't decide on a single coordinate system!
and do the same for green and blue.
edit: also see my comment to the question about using zeros with no class specified.
  11 Kommentare
KSSV
KSSV am 17 Nov. 2016
Dear friend then type it..what is stopping you?
Prachi Sharma
Prachi Sharma am 17 Nov. 2016
Bearbeitet: Prachi Sharma am 17 Nov. 2016
This will also work right?
%for redchannel
mask_r1=(redchannel<x);
newm1=uint8(mask_r1);
mult1=newm1.*redchannel;
%for greenchannel
mask_r2=(greenchannel<y);
newm2=uint8(mask_r2);
mult2=newm2.*greenchannel;
%for blue channel
mask_r3=(bluechannel<z);
newm3=uint8(mask_r3);
mult3=newm3.*bluechannel;
%or ing the result
ora=(newm1|newm1)|newm3;
uora=uint8(ora);
figure
imagesc(uora)
title('Ored result of all the threshold values');

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 17 Nov. 2016
Try something like this:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Make mask where ANY of the color channels is saturated.
saturatedPixels = redChannel == 255 | greenChannel == 255 | blueChannel == 255;
% Make red channel 255, and other color channels 0 where there is saturation
redChannel(saturatedPixels) = 255;
greenChannel(saturatedPixels) = 0;
blueChannel(saturatedPixels) = 0;
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
imshow(rgbImage);
Now your RGB image will be pure red wherever ANY pixel of ANY color channel is saturated (value of 255). For an image in the range 0-1, use 1 instead of 255. For a uint16 image, use 65535 instead of 255.

Community Treasure Hunt

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

Start Hunting!

Translated by