Remove Background using OpenCV in C++ - image

Sorry for lengthier explanation. As am new to Open want to give more details with example.
My requirement is to find the delta of 2 static images, for this am using the following technique:
cv::Mat prevImg = cv::imread("prev.bmp");
cv::Mat currImg = cv::imread("curr.bmp");
cv::Mat deltaImg;
cv::absdiff(prevImg,currImg,deltaImg);
cv::namedWindow("image", CV_WINDOW_NORMAL);
cv::absdiff(prevImg,currImg,deltaImg);
cv::imshow("image", deltaImg);
And in the deltaImg, am getting the difference between the images, but it includes the background of the first image also. I know i have to remove the background using BackgroundSubtractorMOG2, but am unable to understand this class usage as most of the examples are based on webcamera captures.
Please note that my images are static (Screen shots of the desktop activity).
Please guide me in resolving this issue, some sample code will be helpful.
Note I want to calculate delta in RGB.
Detailed Explination:
Images are at : https://picasaweb.google.com/105653560142316168741/OpenCV?authkey=Gv1sRgCLesjvLEjNXzZg#
Prev.bmp: The previous screen shot of my dektop
curr.bmp: The current screen shot of my desktop
The delta between the prev.bmp and curr.bmp, should be the startup menu image only, please find the image below:
The delta image should contain only the startup menu, but even contains the background image of the prev.bmp, this background i want to remove.
Thanks in advance.

After computing cv::absdiff your image contains non-zero values for each pixel that changed it's value. So you want to use all image regions that changed.
cv::Mat deltaImg;
cv::absdiff(currImg,prevImg,deltaImg);
cv::Mat grayscale;
cv::cvtColor(deltaImg, grayscale, CV_BGR2GRAY);
// create a mask that includes all pixel that changed their value
cv::Mat mask = grayscale>0;
cv::Mat output;
currImg.copyTo(output,mask);
Here are sample images:
previous:
current:
mask:
output:
and here is an additional image for the deltaImg before computing the mask:
Problems occur if foreground pixels have the same value as background pixel but belong to some other 'objects'. You can use cv::dilate operator (followed by cv::erode) to fill single pixel gaps. Or you might want to extract the rectangle of the start menu if you are not interested in all the other parts of the image that changed, too.

Related

Is there another way to go back to the rgb image to extract the inicial countour in the registered image?

I already made the image registration, and now I want to apply the registered image in rgb because I need to extract the countourr. As the imregister just work with grayscale images I converted my image to grayscale,but now I canĀ“t find the intensity value of the contour to find the contour indexes. Wat kind of algorithm does imregister applies to the image, tochange the intensity value of the pixels? Or there is another way to go back to the rgb image to extract the inicial countour in the registered image? Does anyone have any sugestion?
There is my matlab code :
% Algorithm for image validation
% Open the two images which will be compared
name2=input('Image name ( automated segmentation) ','s');
img_automated=imread(name2,'png');
figure (1), imshow(img_automated), title('Image automated')
name=input('Image name ( manual segmentation) ','s');
img_manual=imread(name,'png');
img_manual_gray=rgb2gray(img_manual);
%img_manual_gray=img_manual(:,:,3);
figure (2), imshow (img_manual),title('Image manual')
img_automated_gray=rgb2gray(img_automated);
%img_automated_gray=img_automated(:,:,3);
%img_double=im2double(img_automated_gray);
figure (3), imshow (img_automated_gray), title (' Image converted to double ');
%img_automated_gray2=rgb2gray(img_automated);
% View images side by side
figure (6), imshowpair(img_manual,img_automated_eq,'diff')
title('Images overlap')
%% Configure parameters in imreconfig
[optimizer,metric]=imregconfig('Multimodal');
%% Default registration
registered=imregister(img_automated_gray,img_manual_gray,'rigid',optimizer,metric);
%tform = imregtform(img_automated,img_manual,imref2d,'affine',optimizer,metric)
figure(7), imshowpair(registered, img_manual_gray,'falsecolor'); title('Default registration');
figure(8), imshowpair(registered, img_manual_gray,'montage','Scaling','independent'); title('Default registration');
figure(9), imshow(registered);
C = imfuse(registered,img_automated);
figure(21);imshow(C);
%%%%%%%%%%%%%%% Here%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%I tried this process to recover the transformation applied in the registered image,and them aplly this in the initial automated rgb image,but B isn't the same as the registered image. Any suggestions??
tform = imregtform(img_automated_gray,img_manual_gray,'rigid',optimizer,metric);
B = imwarp(img_automated,tform);
figure(22);imshow(B);
Links to the images:
https://www.dropbox.com/s/xbanupnpjaaurj5/manual.png?dl=0 (manual- rgb)
https://www.dropbox.com/s/6fkwi3xbicwzonz/registered%20image.png?dl=0
You are on the right track with your use of imregtform. The missing piece is that imwarp, by default, chooses an output grid with the same resolution as the input grid with limits big enough to capture the entire output image.
In most registration cases, this is not what you want. What you want, is to specify the 'OutputView' Name/Value to specify the resolution and limits that you want in the output grid. In many registration use cases, this is the resolution/limits of the fixed image:
tform = imregtform(img_automated_gray,img_manual_gray,'rigid',optimizer,metric);
B = imwarp(img_automated,tform,'OutputView',imref2d(size(img_manual_gray)));
figure(22);imshow(B);

Matlab gui image incorrectly blue

I am creating a GUI containing an image using the following code:
try
Imagenamehere = imread('Imagenamehere.jpg');
axes(handles.Logo)
image(Imagenamehere)
set(gca,'xtick',[],'ytick',[])
catch
msgbox('Please download all contents from the zipped file into working directory.')
end
The image shows up but for some reason is completely coloured blue as if put through a blue filter. I don't think it would be wise to upload the image but it is a simple logo coloured black and white.
Anyone know what could be causing this?
Check the size, type (probably uint8) and range of your image. It sounds like for some reason your images are being displayed with colormap as jet (the default), and possibly also that your range is not what MATLAB expects (e.g. 0 to 1 not 0 to 255), resulting in all your values being relatively low (blue on the jet colormap).
"black and white" is just one way of interpreting an image file which contains only two colors. MATLAB makes several assumptions when you pass data into a display function like image. If you don't specify colormap and image data range, it will make a guess based off things like data type.
One possibility is that your logo file is an indexed image. In these cases you need to do:
[Imagenamehere map] = imread('Imagenamehere.jpg');
colormap(map);

NSCursor images on a retina display

I am trying to modify the default I-beam cursor image. I'm using [[[NSCursor IBeamCursor] image] representations], passing each one through a CIFilter and adding it to a new image. However, the resulting cursor looks as though it is rendering the low-resolution images.
The High Resolution Guidelines say:
For custom cursors, you can pass a multirepresentation TIFF to the NSCursor class method initWithImage:hotSpot:.
So I would expect this to work. Additionally, if I get the -TIFFRepresentation of the original image and my modified image, and write them to disk, they both look like multi-page TIFF files with the same size images. What could I be doing wrong?
I have a somewhat-temporary solution: manually call -setSize: on each image representation, dividing the pixel height and width by the screen's scale factor. However, this technique doesn't seem like it will work ideally with multiple screens.
You're right on. I've been debugging this all day and I'm pretty sure I've got it nailed. I'm not doing exactly the same thing you are (my images are loaded from a file) but the end result is exactly the same.
The trick is to set the first representation of the multi-representation image to the non-retina size. If you are loading your cursors from an image file, you must take this extra step to adjust the size of the representations to match. It doesn't work 'out-of-the-box' as you would expect.
I've tested this on a machine with two monitors and dragging the window from the retina display to the non-retina display acts as it should, displaying the high/low resolution images for the cursor.
I had a similar problem a while ago: I had my cursor in a PDF, and it always drew as if it was a pixel image at 1:1 size, blown up. There's a solution to that in NSCursor: Using high-resolution cursors with cursor zoom (or retina).
Maybe someone can use that technique to solve this problem? My guess is creating an image with the same size but a different CTM marks it as the same size but Retina. What #jtbrandes is doing probably marks it as a different size and non-Retina. So you're effectively losing the scale factor information. If you create an image with a CTM in the hints, maybe you can draw the filtered images into it and it'll be detected right.

Using opencv in Win32 application for image show

Is it possible to output images so that they all will be inside a single window?
Before, I used to output data using only opencv functions:
cvNamedWindow("Image 1");
cvShowImage("Image 1", img);
So I change image, then call: cvShowImage function and so on.
But If I want to look at more than one image, then every new image needs its own window to be shown there And what I want is to put every such an output opencv's window inside one big main window.
Is it possible to do it? And how?
You will have to construct a new image and place each img into it. I don't think there's a builtin function like MATLAB's subplot. I recommend using the ROI functions to quickly copy an image into a region-of-interest (ROI) of the big image (which holds the others).
You can show as many images as you want on a single window using hconcat function.
Let us suppose your original image was
Mat frame;
Now clone or make a copy of this image using
Mat frame1 = frame.clone();//or
Mat frame2;
frame.copyTo(frame1);
Now let us suppose your output images are
Mat img1,img2,img3,img4;
Now if you want to show images horizontally, use
hconcat(img1,img2,frame1)//hconcat(input_image1,input_image2,destination_image);
And if you want to show images vertically, use
frame2.push_back(img1);//main_image.push_back(image_to_be_shown_below);
This process processess images one at a time, so if you want to show 4 images side by side, you have to call this function 4 times as in
hconcat(img1,img2,frame1);
hconcat(frame1,img3,frame1);
hconcat(frame1,img4,frame1);
imshow("Final Image",frame1);
NOTE:
Clone process is done because the images have to be of same size.
Enjoy...

How to add image in MATLAB GUI?

I want to switch back and forth between two images, like blinking: 1 second for the first image and one second for second image.
I'm not totally sure of what you want to do (specifically what type of images you are trying to display), but here's some sample code that may do what you want:
image1 = imread('cameraman.tif'); % Load a test image
image2 = imread('circles.png'); % Load another test image
hAxes = gca; % Get a handle to the current axes
for iLoop = 1:5, % Loop five times
imshow(image1,'Parent',hAxes);
pause(1);
imshow(image2,'Parent',hAxes);
pause(1);
end
I used the general function IMSHOW, but this sometimes changes other properties of the figure/axes and that may not be to your liking (since you mention adding this to an existing GUI). You may want to use the IMAGE function instead. Also, instead of the for loop you could use a while loop that stops switching images when a condition is met (such as a button press).
How are your images stored in Matlab? As a matlab movie or a 3 or 4 dimensional matrix depending on if the images are color or grayscale. Also, if you have the image processing toolbox, implay and immovie. Another option assuming that your images are in a mxnx3xk (rgb color) or a mxnxk (gray scale) matrix. Then the following should work. Assuming the following
Img - matrix storing image data either with dimensions mxnx3xk
or mxnxk
handles.imageAxes -
handle for the axis you want to
display the image (set the tag of the
axis to imageAxes in GUIDE)
Now you can loop through Img
for i=1:k
% display the i^th image use `Img(:,:,i)` for a gray scale stack
image(Img(:,:,:,i),'parent',handles.imageAxes);
pause(1) % pause one second
end
that's it.

Resources