2d index to 3d array

조회 수: 2 (최근 30일)
Ben
Ben 2013년 4월 10일
Hi, I'd like to set several pixels in a base image to a color. These pixels are given as a 2d array (determined from a threshold in greyscale). Its fairly easy to do in grayscale:
im= ('something.pgm') peaks = im>200 baseimage(im)= 150;
but if I have a color image I need to set the three colours (third dimension)
something like: baseimage(im,:) = [0,0,1] which obviously doesn't work, but there must be a neat way to do this.

채택된 답변

Walter Roberson
Walter Roberson 2013년 4월 10일
[r, c, b] = size(baseimage);
rgb = [0 0 1]; %fill color
idx = find(im);
baseimage(idx) = rgb(1);
baseimage(idx+r*c) = rgb(2);
baseimage(idx+2*r*c) = rgb(3);
  댓글 수: 1
Ben
Ben 2013년 4월 10일
Works perfect, thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ahmed A. Selman
Ahmed A. Selman 2013년 4월 10일
First note that adding a third dimension that is interpreted as (color) in a grayscale (2-D matrix) image is a logically false task, yet it is mathematically possible. I think there have been some discussions in this forum about this issue.
As for your question, try using imwrite function with a color map argument, that's if you want to deal with coloring grayscale images, im, as:
imwrite(im,colormap(hot(100)),'myImage','jpg'));% hot is one type of many colormaps. Try others for different results.
If you want to manually add a 3rd dimension to a 2-D matrix (im), use concatenation commands like
[a,b]=size(im);
baseimage=cat(3,im,eye(a,b));% adds a unit matrix as a third dim. Use ones(a,b) to add ones matrix.
  댓글 수: 1
Ben
Ben 2013년 4월 10일
편집: Ben 2013년 4월 10일
Sorry, maybe my question was not clear. I have converted a grayscale to color like this:
backim=cat(3,baseim,baseim,baseim);
Which gives a grey image that can have colour.
Now I need to colour some areas of the image which are defined in another image 'im' (a 2d binary array)
I'd like to be able to choose the fill color.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by