Why do I write as Ib(1:478,31:565)(~BW) = 255

27 ビュー (過去 30 日間)
xie
xie 約14時間 前
Ib(1:478,31:565)(~BW) = 255
Ib is a gray image. I want to set a fixed black region as blue, but this code is wrong.
Do we have simple method to finish it?
  1 件のコメント
xie
xie 5分 前
I don't want to assign Ib(1:478,31:565) to new variable to meet request.

サインインしてコメントする。

採用された回答

DGM
DGM 約7時間 前
編集済み: DGM 約6時間 前
There are various ways, including expanding the mask, or using more purpose-built tools, but if you're trying to apply an undersize mask to a rectangular sub-image, maybe it'd be best to just show it like this, since it demonstrates a number of concepts that can be rearranged and applied as is truly needed.
% this is a single-channel (gray) image in uint8
inpict = imread('cameraman.tif'); % 256x256x1
imshow(inpict)
% say we have some smaller logical mask
% ignore the ==0 here. that's just a consequence of the image i'm repurposing
mask = imread('150mk.png') == 0; % 150x150x1
imshow(mask)
% if we want the output to contain color,
% let's start with expanding the image as needed
if size(inpict,3) == 1
outpict = repmat(inpict,[1 1 3]);
else
outpict = inpict;
end
% we want to apply the mask to some region within the image
% we might choose to just extract that region
offset = [10 10]; % [y x]
sz = size(mask,1:2);
roi = outpict(offset(1):offset(1)+sz(1)-1, ...
offset(1):offset(2)+sz(2)-1,:);
% we can apply the mask to that sub-image
fgcolor = [0 0 1]; % unit-scale float
for c = 1:3
thischannel = roi(:,:,c);
thischannel(mask) = im2uint8(fgcolor(c));
roi(:,:,c) = thischannel;
end
% we can put the roi back into the image if we want
outpict(offset(1):offset(1)+sz(1)-1, ...
offset(1):offset(2)+sz(2)-1,:) = roi;
% show the result
imshow(outpict)
Not sure if that covers all the bases, but maybe it's a start. I know you don't want to assign the subimage to a new variable, but you're going to have to do something, since the image an mask have mismatched geometry, and the input and output have mismatched depth.
EDIT: Like I said, there are other ways. This example uses different approaches to addressing both of the above issues.
% this is a single-channel (gray) image in uint8
inpict = imread('cameraman.tif'); % 256x256x1
% say we have some smaller logical mask
mask = imread('150mk.png')==0; % 150x150x1
% instead of working on the sub-image to fix the geometry mismatch
% just expand the mask to fit the image
offset = [10 10]; % [y x]
sz = size(mask,1:2);
fullmask = false(size(inpict,1:2));
fullmask(offset(1):offset(1)+sz(1)-1, ...
offset(1):offset(2)+sz(2)-1) = mask;
% use simple multiplicative composition to construct the output
% in modern versions, this lets us avoid the problem of mismatched depth
fgcolor = [0 0 1]; % unit-scale float
outpict = im2double(inpict).*(1-fullmask) ... % do the composition
+ permute(fgcolor,[1 3 2]).*fullmask;
outpict = im2uint8(outpict); % assuming the output should be uint8
% show the result
imshow(outpict)
Alternatively, so long as the image/mask geometry matches, the final composition could be done using tools made for the purpose.
% IPT imoverlay() can do solid-color fills, given a logical mask
fgcolor = [0 0 1]; % unit-scale float
outpict = imoverlay(inpict,fullmask,fgcolor);
  2 件のコメント
xie
xie 約4時間 前
@DGM, thank you!
I just want to pickup golden pattern. change the red to blue in below picture.
but Ib(1:478,31:565)(~BW) = 255 , it can't work in matlab.
as your final reminder, I can use function: imoverlay. It's very easy and simple. but the effect is changed all color but my pattern I need.
I0=imread("IMG_0307.JPG");
BW = imbinarize(im2gray(I0),"adaptive");
BW = uint8(BW);
R=I0(:,:,1);G=I0(:,:,2);B=I0(:,:,3);
Ir = R.*BW;Ig = G.*BW;Ib = B.*BW;
% Ib(1:478,31:565)(~BW) = 255;
Ie = cat(3,Ir,Ig,Ib);
imshowpair(I0,Ie,"montage");
I1=imoverlay(I0,~BW,[0 0 1]);
imshow(I1);
DGM
DGM 約4時間 前
編集済み: DGM 約3時間 前
Yeah. You don't need to do a color fill, but a color adjustment. The composition step will still be part of the process, but imoverlay() isn't the tool for the job.
MATLAB doesn't have purpose-built tools for doing hue adjustment, but there are indirect ways to do it, and there are purpose-built tools on the File Exchange.
Getting clean edges on the composition may be a challenge
% the image
I0 = imread("IMG_0307.JPG");
% a mask of the same geometry
% this can be binarized or soft edged
% and it can be based on graylevel intensity
% or it can be based on color properties (e.g. hue and saturation)
BW = imbinarize(im2gray(I0),"adaptive");
% adjust the HSV hue on a copy
hadjust = -0.30; % adjust me
hsvcopy = rgb2hsv(I0);
hsvcopy(:,:,1) = mod(hsvcopy(:,:,1)+hadjust,1);
adjusted = hsv2rgb(hsvcopy);
% compose the two
mask = im2double(BW); % make sure it's unit-scale
outpict = im2double(I0).*mask + adjusted.*(1-mask);
outpict = im2uint8(outpict);
% show the result
imshow(outpict)
The same adjustment and composition can be done more succinctly with purpose-built tools.
% ... same as before ...
% adjust the color on a copy
% this can adjust any component of color
% within any selected color model, not just HSV
adjusted = imtweak(I0,'hsv',[-0.30 1 1]); % MIMT
% compose the two
outpict = replacepixels(I0,adjusted,BW); % MIMT
% show the result
imshow(outpict)
These examples cover doing masked color adjustment using various tools and techniques:
pink strawberries (answer as comment; HSV segmentation & adjustment; no MIMT)
naive channel swap, hard masked filling/tweaking, lin masked colorization (color chips)
triangle of colored dots; links to other answers
This example is non-masked color adjustment by various methods, showing the influence each approach has on the perceived change of independent color properties. This same lesson applies to masked adjustments.
popsquares example (hue rotate, hue replace, solid fill; mostly MIMT)

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 約6時間 前
編集済み: Walter Roberson 約6時間 前
temp = Ib(1:478,31:565,:);
temp(~repmat(BW,1,1,ndims(temp))) = 255;
Ib(1:478,31:565,:) = temp;
  1 件のコメント
xie
xie 21分 前
temp matrix size is mismatch with BW.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by