Matlab color vector change
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Write MATLAB codes that change the MATLAB Lena image, x=300:500 and y=300:500 pixels to black and red.
2 comentarios
Jan
el 1 de Abr. de 2022
This is a command and not a question.
AS usual for homework questions: Please post, what you have tried so far and ask a specific question.
DGM
el 1 de Abr. de 2022
What exactly is meant by "change ... pixels to black and red"?
Does that mean that the pixels in that region should be one of [1 0 0] or [0 0 0] (a binary image)?


Does it mean that they should be one of any of the colors between black and red (a colormapped image)?


Is the replacement region dependent on the image content, or is it just being filled with some solid color or pattern?


Respuestas (2)
Image Analyst
el 1 de Abr. de 2022
Hint:
[r,g,b] = imsplit(rgbImage);
x=300:500;
y=300:500
g(y, x) = 0;
That's only a start but you should be able to figure out what to do from there, i.e. set values and apply to the other color channels.
To recombine, use cat(3,r,g,b)
2 comentarios
ALEXANDRA-CRISTINA ALBRECHT
el 5 de Abr. de 2022
Hello, @Image Analyst, can you give me your e-mail?
DGM
el 5 de Abr. de 2022
Unless I'm mistaken, everyone is still waiting for you to do what's expected of anyone asking for help with homework:
- Show what efforts you've made to solve the problem yourself
- Clarify the problem description when asked
It's not typical that anyone will provide private help, since it defeats the purpose of a public forum, and in matters like homework, it comes off as surreptitious.
DGM
el 18 de Abr. de 2022
I'm going to assume that this homework assignment is past-due, so I'm just going to dump the pending answer so I can clear out my notes.
The above comment was constructed using MIMT tools, and I can't be bothered to rewrite this to reinvent those wheels.
As the ROI is rectangular, grid-aligned region, the composition is a trivial matter of array indexing. The majority of this exploration is what exactly it means to change the image to "black and red".
Assuming "black and red" means a binarized image region containing only [0 0 0] and [1 0 0]
inpict = imread('lena.tif');
v = 300:500;
lv = numel(v);
% do a simple binarization in red
sample = inpict(v,v,:); % extract a sample region
sample = rgb2gray(sample); % reduce it to a grayscale representation
sample = (sample>128)*255; % threshold it somehow
sample = cat(3,sample,zeros(lv,lv,2)); % pad the G,B channels with zero
% reassemble the image
iapict = inpict;
iapict(v,v,:) = sample;

% binarized, but dithered
sample = inpict(v,v,:); % extract a sample region
sample = zfdither(mono(sample,'y'))*255; % binarize a grayscale copy by dithering
sample = cat(3,sample,zeros(lv,lv,2)); % pad the G,B channels with zero
% reassemble the image
iapict = inpict;
iapict(v,v,:) = sample;

Yeah, that's dithered. You'll have to right click and view the image at full scale to see.
Assuming it can be any sort of shade between the two, you can try to just omit green and blue content:
% just omit green and blue
iapict = inpict;
iapict(v,v,2:3) = 0;

... but the results are likely to have poor local contrast compared to something like this:
% grayscale, but in red
sample = inpict(v,v,:); % extract a sample region
sample = mono(sample,'y'); % a grayscale representation based on all channels
sample = sample*(255/max(sample(:))); % maximize peak R so it doesn't look so dim
sample = cat(3,sample,zeros(lv,lv,2)); % pad the G,B channels with zero
% reassemble the image
iapict = inpict;
iapict(v,v,:) = sample;

Perhaps "red and black" could mean any red tint or shade. You might try colorization with a 'color' blend mode:
% do a 'color' blend
sample = inpict(v,v,:); % extract a sample region
cpict = colorpict([lv,lv,3],[255 0 0],'uint8');
sample = imblend(cpict,sample,1,'colorlchab'); % there are other 'color' blends available
% reassemble the image
iapict = inpict;
iapict(v,v,:) = sample;

... or you could use a colorization tool:
% another colorization tool
sample = inpict(v,v,:); % extract a sample region
sample = gcolorize(sample,[0 70 0]); % not 100% saturated
% reassemble the image
iapict = inpict;
iapict(v,v,:) = sample;

Note that both of these have better preservation of the brightness in the ROI. Preserving region brightness is simply not possible if colors are restricted to the path from [1 0 0] to [0 0 0].
Then again, who knows if the region content is even supposed to be preserved?
% fixed inputs
sample = inpict(v,v,:); % extract a sample region
sample = freecb([lv lv],20)*255; % a checkerboard pattern
%sample = perlin([lv lv])*255; % perlin noise
sample = cat(3,sample,zeros(lv,lv,2)); % pad the G,B channels with zero
% reassemble the image
iapict = inpict;
iapict(v,v,:) = sample;


Again, most of these examples use MIMT tools, though some cases can be trivially avoided.
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!