Problem in plotting the matrix over an image.
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2016 年 11 月 16 日
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 ?
回答 (3 件)
KSSV
2016 年 11 月 16 日
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 件のコメント
KSSV
2016 年 11 月 16 日
You try it out...this is the way the matrix size of x,y making equal to indx. Do consider the Guillaume comments.
Guillaume
2016 年 11 月 16 日
編集済み: Guillaume
2016 年 11 月 16 日
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 件のコメント
Image Analyst
2016 年 11 月 17 日
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!