Adjust Brightness of Image part - image

I am new to Matlab and to Image Processing as well, I know we can change image brightness by following formula if my image is I
newImg=imadjust(I, [low_in high_in], [low_out,high_out]);
it adjust all pixel's value of image but how can I do it on some part of image, like I detected face in the image now I want change its brightness how can I do it using imadjust.
Edit
I have detected area in a binary mask.
I done it please see the answer and also the reference in the answer.

I done it,
Well I wanted to change the brightness of some area of the image, the area which I wanted was calculated by a binary mask mask, So I did, First I simply change the brightness of my input Image I, and store the result in newImg like this
newImg = imadjust(I, [low_in high_in], [low_out,high_out]);
then apply mask on my orignal image and store value of masked area of newImg like this
I(mask) = newImg (mask);
Reference
I think its informative and not to bore anyone, If you guys wont motivate me then I cant learn, as people closed my thread just because I was asking questions, and I am using same database of Image for different type of projects.

%Suppose these are the coordinates of the rectangle in which the face is detected%
%You can do the following to adjust the brightness of that region
topLeft = 10;
topRight = 50;
bottomLeft = 50;
newImg = I;
newImg(topLeft:bottomLeft,topLeft:topRight) = imadjust(newImg(topLeft:bottomLeft,topLeft:topRight), [low_in high_in], [low_out,high_out]);

Related

imtranslate command matlab show me a black image

I'm trying to translate an image read through imshow function, but the output is a black image.
Here's the code:
fingers=imread('thumb-index.png');
[nrows,ncols] = size(fingers);
RI = imref2d(size(fingers));
h = imshow(fingers,RI);
grid on
x_center=ncols/2;
y_center=nrows/2;
x_orig=ncols-x_center;
y_orig=nrows-y_center;
[fingers_translate,RF]=imtranslate(fingers,RI,[x_orig,y_orig]);
figure
h=imshow(fingers_translate,RI);
grid on
My target is translating the origin of coordinate system in the center of the image.
Thanks!
you pushed the image out of frame. To see translation effect try:
x_orig=100;
y_orig=100;

How to make remap of particular area in OpenCV

I am facing the performance drop issue. I made some research and it seems that remap function takes too much time. Image size is VGA, but interesting area has about about 1/4 of this area. Therefore, I want to use remap() only for this region and finally get image with about 1/4 of VGA area.
This is image input in VGA resolution -green rect is trackableArea Rect
Desired output but in VGA
Generated by:
remap(originalCornersSamples[i], rview, map1, map2, INTER_NEAREST);
When I try to make remap only on specific area:
remap(frame_bgr, rview, map1(trackableArea), map2(trackableArea), INTER_NEAREST);
I got as expected - stretched desired image with desired resolution of the trackableArea rect.
map1 and map2 were generated from getPerspectiveTransform to get only TV screen from the input image. The trackableArea is a Rect like here (green lines):
Any ideas how to make it possible or how the remap() should look like?
I answer to myself :) So this helped:
resize(map1,modified,Size(trackableArea.width,trackableArea.height), 0, 0, INTER_CUBIC );
remap(frame_bgr, rview, modified, map2(trackableArea), INTER_NEAREST);

Adjust figure when using plt.gcf

I have a image plotted using
plt.gcf()
and the command
plt.gca().add_artist
I edited the axes with
fig.gca(xlim=[-5,5],ylim=[-8,2])
The problem is that when I save the image, it gives me:
I want that in any pair of axes, I get the exact image and not a stretched image.
ax = fig.gca()
ax.set_aspect('equal')

How to place foreground images at specified locations over background image in Matlab?

I am working on a task in which I have some objects like humans and toys and food which are foreground images/objects and I have a background image, for eg. like a park. I need to place foreground images/objects at specified locations over background image. I am using Matlab.
I was able to place a foreground image over background image, but its positioned in the center - not at a specified position.
How can I place foreground images at specified locations over background image using Matlab ?
My code is as follows:
figure1 = figure;
ax1 = axes('Parent',figure1);
ax2 = axes('Parent',figure1);
set(ax1,'Visible','off');
set(ax2,'Visible','off');
[a,map,alpha] = imread('foreground.png');
I = imshow(a,'Parent',ax2);
set(I,'AlphaData',alpha);
imshow('Background.jpg','Parent',ax1);
My images:
1) What I want:
2) What I'm getting:
Here is a simple solution using ginput in which the user clicks on a figure (here only once) and you fetch the coordinates of the point(s). In this example, there is a message box asking the user to select a point, then the foreground image is drawn over the background image. Notice that in my example I replace the pixels from the background image with those of the foreground. i.e. I do not use alpha and transparency. Hope that's fine for you; if not please tell me.
In the example, my 'background' image is the peppers image that ships with Matlab and the 'foreground' image is the pears image that also ships with Matlab. I use a small portion of the image for the demonstration.
Here is the code:
clear
clc
close all
%// Set up background image (peppers.png) and foreground image (part of
%// pears.png)
BackgroundImage = imread('peppers.png');
DummyForeground = imread('pears.png');
ForegroundImage = DummyForeground(50:200,50:200,:);
%// Get size of foreground image
[rowFore,colFore,channelFore] = size(ForegroundImage);
figure
imshow(BackgroundImage);
hMsg = msgbox('Select an anchor point for foreground image','modal');
uiwait(hMsg)
This looks like this:
%// Use ginput to prompt user to select a single point (i.e. the 1 in
%// brackets).
[x,y] = ginput(1);
Calling ginput results in the following:
x = round(x);
y = round(y);
%// Important!
hold on
%// Replace pixels of background image with foreground image.
BackgroundImage(y:y+rowFore-1,x:x+colFore-1,:) = ForegroundImage;
imshow(BackgroundImage);
And finally the background image with the foreground image:
Note: It looks like there is a shift between the cursor and the actual image placed; that happened when I took the screenshot haha it's not a bug :)
Now if you would like to add many images in the foreground, you could easily modify the code using multiple points for ginput, calling it like so:
[x,y] = ginput %// Indefinite # of points)
or [x,y] = ginput(SomeNumber) %// Any number you want
and add appropriate images for every point you selected.
Hope that was clear enough and it gets you started!

read image but show strange colour

I have read a image like that
a = imread('test.jpg');
image(a)
what the test.jpg is:
but after the image function
the result is:
and I don't know why it show that?
Because I want to crop some part, so I have to see the image shown.
How to fix it by showing the human face by image?
You are using image command to display an image. From here: "image creates an image graphics object by interpreting each element in a matrix as an index into the figure's colormap or directly as RGB values". Since you are providing a 2-D matrix, each element is interpreted as the index to the figures colormap. You can see the figure's colormap by using
c_map=colormap;
Also, the axis is set to square, therefore you see a circle instead of an ellipse. Use imshow(a,[]) to display the grayscale image as desired.
You image data might be in a color map instead. try [a,cmap] = imread(...). If cmap is not empty, a is indices into cmap, and cmap contains the actual colors.
Use img = cat(3,cmap(a,1),cmap(a,2),cmap(a,3)) to get your image, and show it with image(img).
Note that using imagesc might be misleading in this case as it will still show something that looks like your image when simply doing imagesc(a). This as different pixels colors are associated with different index-values in a.

Resources