Editing Images In Matlab

31 views (last 30 days)
John hope
John hope on 26 Apr 2019
Edited: DGM on 27 Mar 2024 at 21:19
Hi all
I was wondering if there was any way of editing an image in Matlab.
My aim is to import an image (.jpg) and based on certain criteria automatically draw a Red box in the middle of th eimage and fill the box with red color and then adjust the transparancey so that you can still see the contents of the image.
Below is an example of what i hope to achieve:-
pppp.JPG
Does anyone have any ideas?
Thank you.

Accepted Answer

Image Analyst
Image Analyst on 27 Apr 2019
Edited: Image Analyst on 27 Apr 2019
Just add an offset to the red channel in the zone you want
rgbImage = imread(filename);
rgbImage(row1:row2, col1:col2, 1) = rgbImage(row1:row2, col1:col2, 1) + 50; % Make red channel 50 gray levels brighter.

More Answers (1)

DGM
DGM on 25 Oct 2022
Addition is probably fine in this case, but it is my hobby to elaborate.
I look at this problem as a simple image blending/composition task. Regardless of how the ROI selection is handled, the goal is to take a background image and combine it mathematically with an overlaid red color. If that sounds broad and nonspecific, that's because there's a lot of different methods for combining two images -- and there are a lot of different ways in which their parameters (if any) can be adjusted.
The general characteristics of a blend mode should describe its suitability for a given task. There are modes which strictly lighten and modes which strictly darken the background. There are modes which do both. Most modes have conditions under which they produce no effect (a neutral response). If the question of which blend mode to use is challenging, back up and ask what exactly is the desired effect. It's often not an easy question to answer either, but it at least provides guidance.
We have an example image of black content (lines, text) on a nominally-white background. In this example is overlaid a red rectangular region by simple alpha blending. If we assume that the original alpha were 0.5, then the foreground color would be approximately [250 45 50]. Given the unknown alpha, printthrough, and JPG mutilation, take that tuple with a grain of salt.
Still, the image polarity is dark-on-light. One reasonable set of requirements might start like this. The overlaid rectangle should present with clearly-discernable boundary contrast against the surrounding background, and should do so without obscuring the object content beneath. Let's look at a few basic options.
Addition (linear dodge)
Addition is exactly what it sounds like. The pixel values are just added. This method can only lighten the background. Bright BG regions are offset just the same as dark BG regions. This method will have strong influence over dark regions and no influence over strictly white BG regions.
Color Dodge
Color dodge is similar in effect to linear dodge, but the operation is scaled such that the influence over dark regions is reduced. This method will have no influence over strictly white or black BG regions.
Screen
This is the complement of multiplication. This can only lighten the background, and so has no influence over white BG regions. Unlike the prior two dodge modes, screen does not result in local contrast loss due to clipping where bright FG,BG regions coincide.
Multiply
Multiplication is what it sounds like. Assuming we're dealing with unit-scale images, they're just multiplied. This can only darken the background, and has no influence over black BG regions. As with screen, multiply blending doesn't result in clipping. If you were dealing with strictly black text on a strictly white background, this would be the obvious simple choice.
Soft Light
Softlight blends are subtle bidirectional modes. They can both darken and lighten the background. Depending on how varied the background is, a bidirectional behavior might be desirable to help maintain the edge contrast around the rectangle. Softlight blends will have no influence over strictly white or black BG regions, and they will not cause any clipping.
Simple alpha blending
This is what is used in the given example. While I'm not using the same color here, the idea is the same. The result is simply a weighted average of the foreground and background images. Since we're averaging the background and its object content with a flat foreground, it should stand to reason that as alpha increases, the object contrast in the ROI decreases. The only case in which alpha blending has no influence is if the BG is the same color as the FG.
% get an image
inpict = imread('cameraman.tif');
inpict = repmat(inpict,[1 1 3]); % force the image to be RGB
% pick a rectangular region that's proportional to the image geometry
w = 0.5; % specify a box proportions
sz = [size(inpict,1) size(inpict,2)];
hw = w*sz/2;
rows = round(sz(1)/2-hw(1)):round(sz(1)/2+hw(1));
cols = round(sz(2)/2-hw(2)):round(sz(2)/2+hw(2));
% extract a sample
roisample = inpict(rows,cols,:);
% blend some images
% note that these all rely on implicit expansion (R2016b or later)
% note that attention must be paid to data class and scaling throughout
% do addition (linear dodge) blend
FG = [0.5 0 0];
outpict1 = inpict;
outpict1(rows,cols,:) = roisample + permute(im2uint8(FG),[1 3 2]);
% do color dodge blend
FG = [0.5 0 0];
outpict2 = inpict;
outpict2(rows,cols,:) = im2uint8(im2double(roisample)./(1 - permute(FG,[1 3 2])));
% do screen blend
FG = [0.5 0 0];
outpict3 = inpict;
outpict3(rows,cols,:) = im2uint8(1-(1-im2double(roisample)).*(1-permute(FG,[1 3 2])));
% do multiply blend
FG = [1 0 0];
outpict4 = inpict;
outpict4(rows,cols,:) = im2uint8(im2double(roisample).*permute(FG,[1 3 2]));
% do softlight blend (simple form)
FG = [1 0 0];
outpict5 = inpict;
outpict5(rows,cols,:) = im2uint8(im2double(roisample).^(2.^(1-2*permute(FG,[1 3 2]))));
% do basic alpha blend
alph = 0.4;
FG = [1 0 0];
outpict6 = inpict;
outpict6(rows,cols,:) = im2uint8((1-alph)*im2double(roisample) + alph*permute(FG,[1 3 2]));
% concatenate and display
imshow([outpict1 outpict2; outpict3 outpict4; outpict5 outpict6])
Note that 'linear dodge' strongly raises the dark regions, whereas 'color dodge' does not. Note that while 'screen' also raises dark regions like 'linear dodge', its smooth behavior helps preserve contrast in lighter regions. 'Softlight' offers subtle boundary contrast without profoundly influencing contrast within the ROI. Simple alpha blending tends to reduce contrast within the ROI, but it does provide good boundary contrast.
Of course, MIMT imblend() can do the same thing with less mess and without the need to remember the math. Let's do the same six examples on a different image:
% get a picture
inpict = imread('printedtext.png');
inpict = imresize(inpict,0.25); % it doesn't need to be huge
inpict = gray2rgb(inpict); % make it RGB
% pick a rectangular region that's proportional to the image geometry
w = 0.6; % specify a box proportions
sz = [size(inpict,1) size(inpict,2)];
hw = w*sz/2;
rows = round(sz(1)/2-hw(1)):round(sz(1)/2+hw(1));
cols = round(sz(2)/2-hw(2)):round(sz(2)/2+hw(2));
% extract a sample
roisample = inpict(rows,cols,:);
% some red overlay images
FG100 = colorpict([imsize(roisample,2) 3],[1 0 0]);
FG50 = colorpict([imsize(roisample,2) 3],[0.5 0 0]);
% blend some images
% this is not version-dependent or class-sensitive
% do addition (linear dodge) blend
outpict1 = inpict;
outpict1(rows,cols,:) = imblend(FG50,roisample,1,'lineardodge');
% do color dodge blend
outpict2 = inpict;
outpict2(rows,cols,:) = imblend(FG50,roisample,1,'colordodge');
% do screen blend
outpict3 = inpict;
outpict3(rows,cols,:) = imblend(FG50,roisample,1,'screen');
% do multiply blend
outpict4 = inpict;
outpict4(rows,cols,:) = imblend(FG100,roisample,1,'multiply');
% do softlight blend
outpict5 = inpict;
outpict5(rows,cols,:) = imblend(FG100,roisample,1,'softlighteb2');
% do basic alpha blend
outpict6 = inpict;
outpict6(rows,cols,:) = imblend(FG100,roisample,0.4,'normal');
% concatenate and display
imshow([outpict1 outpict2; outpict3 outpict4; outpict5 outpict6])
While this image is a bit extreme, it helps demonstrate the tradeoffs involved with each method. It also demonstrates that imblend() is a lot easier and more readable than writing a tiny bit of easily-mistaken arithmetic and burying it in a pile of class conversions.
These aren't the only methods one might choose. They're just basic methods. What works best depends on the images in question and what weight you put on object contrast and boundary contrast. If using imblend(), a large selection of other modes is available. If you want to do some other blend without imblend() or third-party tools, you'll have to find the formulae for the modes you want. To that end, imblend() itself is (as far as I know) the single most comprehensive resource on the web. The aforementioned properties are cataloged in its documentation.
For further reading on creating an image "overlay" by blending/compositing, see this answer and its references.
  2 Comments
Image Analyst
Image Analyst on 25 Oct 2022
One might also look at the built-in labeloverlay for transparent overlays or imoverlay for opaque overlays.
DGM
DGM on 25 Oct 2022
Edited: DGM on 27 Mar 2024 at 21:19
For labeloverlay():
% get an image
inpict = imread('cameraman.tif');
% pick a rectangular region that's proportional to the image geometry
w = 0.5; % specify a box proportions
sz = [size(inpict,1) size(inpict,2)];
hw = w*sz/2;
rows = round(sz(1)/2-hw(1)):round(sz(1)/2+hw(1));
cols = round(sz(2)/2-hw(2)):round(sz(2)/2+hw(2));
% create a mask
mk = false(sz);
mk(rows,cols) = true;
% create a colortable (with only one tuple)
CT = [1 0 0];
% reproduce the alpha blend example from before
outpict = labeloverlay(inpict,mk,'transparency',(1-0.4),'colormap',CT);
imshow(outpict)
Note that when using labeloverlay(), the overlay prominence is not controlled by alpha or opacity, but by transparency -- i.e. the complement of opacity/alpha.
For imoverlay():
% get an image
inpict = imread('cameraman.tif');
% pick a rectangular region that's proportional to the image geometry
w = 0.5; % specify a box proportions
sz = [size(inpict,1) size(inpict,2)];
hw = w*sz/2;
rows = round(sz(1)/2-hw(1)):round(sz(1)/2+hw(1));
cols = round(sz(2)/2-hw(2)):round(sz(2)/2+hw(2));
% create a mask
mk = false(sz);
mk(rows,cols) = true;
% create a colortable (with only one tuple)
CT = [1 0 0];
% make an opaque red rectangle
outpict = imoverlay(inpict,mk,CT);
imshow(outpict)
While the prior blending/composition examples made no use of antialiased or soft-edged overlay methods, they're capable of it. Tools like imoverlay() and labeloverlay() are strictly limited to operating on binarized regions, so they can't make use of any antialiasing. I doubt that bothers most people, but I think it's noteworthy.
% get an image and antialiased mask
inpict = imread('cameraman.tif');
mk = imread('cmantifmk.png');
% zoom in on part of the image
st = 35;
en = 150;
inpict = inpict(st:en,st:en);
mk = mk(st:en,st:en);
% create composite images using base tools
op1 = labeloverlay(inpict,mk>128,'transparency',0.6,'colormap',[1 0 0]);
op2 = imoverlay(inpict,mk,[1 0 0]);
% MIMT replacepixels is often convenient for ops with a loose mask
% or without a full FG image
op3 = replacepixels([1 0 0 0.4],inpict,mk); % FG is an RGBA tuple
% but otherwise an RGBA workflow has its flexibility
FG = colorpict(imsize(inpict,2),[1 0 0]);
FG = joinalpha(FG,mk);
op4 = imblend(FG,inpict,0.4,'normal'); % or any other blend mode
op4 = splitalpha(op4);
% upscale the result to make it easier to view
outpict = imresize([op1 op2; op3 op4],2,'nearest');
imshow(outpict)
The other major caveat is one of intent. IPT labeloverlay() is really made for overlaying a label array on an image. It can be used for overlaying a strictly binarized foreground, but from the perspective of the tool, a binary image is just a label array representing a single class. If for any reason the FG image is not binarized, labeloverlay will not protest, but rather, it will blindly interpret any graduated regions as other classes and color them accordingly.
% get an image and antialiased mask
inpict = imread('cameraman.tif');
mk = imread('cmantifmk.png');
% zoom in on part of the image
st = 35;
en = 150;
inpict = inpict(st:en,st:en);
mk = mk(st:en,st:en);
% a colormap
CT = jet(256);
% this FG has some antialiasing
op1 = labeloverlay(inpict,mk,'transparency',0.6,'colormap',CT);
% this FG image was binarized, but has suffered from compression
imwrite(mk>128,'test.jpg')
mk2 = imread('test.jpg');
op2 = labeloverlay(inpict,mk2,'transparency',0.6,'colormap',CT);
% upscale the result to make it easier to view
outpict = imresize([op1 op2],2,'nearest');
imshow(outpict)
In other words, misusing labeloverlay() as a general image composition tool is limited by the fact that it's not a general image composition tool. It's entirely up to the user to make sure they use it within those limitations, as labeloverlay() will gladly create unexpected garbage if this behavior is not understood.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!