How to use OpenCV to process the example picture - c++

I need some suggestion in processing the following image.
Basically, I want to remove the background which is a dark red table and leave the filter (a white background and a gray spot in the middle)
This picture is taken by the camera. I think in a real experiment, the background may not be a single color but the type of filter always be the same.
edit:
The desired object could be anywhere in the picture.
The background could be anything not just the dark red
But the object is always white
I am using OpenCV to process this image, any suggestion about what types of method of OpenCV should be used for this example.
Thanks

Related

Getting Hue value from the peaks in histogram opencv

I am trying to use color information of detection of rectangles. Some of my rectangles are overlapping and with multicolor. I found a solution to detect these rectangles using Hue values. I am checking inRange with Hue values of colors
Orange 0-22
Yellow 22- 38
Green 38-75
Blue 75-130
Violet 130-160
Red 160-179
, but I do not know what exact color is going to be. For example, in one image rectangles can be orange, red, blue and in another image, it can be other colors.
I tried to look histogram, but I would have a background which is not only white or black. So, the histogram is confusing.
If you give me some ideas about how to handle this problem, I will appreciate it.
You can try a brute force approach, where you try all the color ranges, then use findcontours (example) to see if you can find a contour that is possibly a rectangle. If the background is very noisy you can use a minimum size for the contour
(contourArea). You could also check the solidity by dividing the contour area by the area of the minAreaRect, the result for a rectangle (that has good detection) should approach 1.
Whether this could possibly work depends on several factors, and overlapping rectangles will quickly break it.
So if I understand correctly, you have a variety of images, each of which contain multiple rectangles which can be a variety of different colors, and the background of the image is non uniform, and you're trying to segment out the rectangles using a histogram?
Using histograms for image segmentation works best with grey scale images with a uniform background, so that upon seeing the peeks in your histogram you know the primary intensities of the objects you are trying to segment out. This method is not going to translate well to your application because the shapes you are attempting to segment are non uniform in shade, without seeing example images I would probably say this isn't going to work, however you might be able to get away with it if the shade variation of the rectangles is relatively similar... basically if you have rectangles that are 15-30 you might be alright, but if they vary from 20-100 you're going to be out of luck, same goes with variation of the background.
If the background and the rectangles have very clearly defined borders, and the background colors transition VERY smoothly, you may be able to get away with some sort of region growing on the background in order to get a list of all the background pixels and then just set those to black or something in order to allow better analysis of the rectangles in the foreground, but I can only speculate so much with the information you've given in your post

Inkscape: radial opacity gradients on jpgs

I'm trying to create an image where I'd like to include part of a jpg image in the following way: I'd like to have the "outer" part of the jpg at the fringes, so that it is opaque on the edges and transparent in the middle (where the main part of my image is). I've tried the following:
1. Adding a radial opacity gradient to the jpg itself as if it were a vector-based object.
2. Creating an Inkscape object with an analagous gradient and then doing a Clip>Set with the jpg.
Neither of these worked (obviously). Is there a way to do this in Inkscape? all of my attempts have either done nothing, or have only succeeded in changing the opacity of the image uniformly (without a gradient).
The feature you want is called Masking, you can find it right below Clip in the menu in Inkscape.
The darker or more transparent the mask object is, the more it hides from what is below. So you need a gradient that goes from black in the center to white at the outer perimeter.
The rest works just like clipping.

Detecting a photograph (i.e. rectangle) on screen using find contours?

I'm trying to detect a photograph in front of the screen with OpenCV (using a webcam). I am using the following code, which uses findContours() to detect rectangles (which for my purpose would count as a photograph.
https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp
This works well, but findContours expects a white background against black, so the image needs to be inverted. I tried changing the threshold, but i still can't get it to detect a photograph.
Am i going about this the right way or would there be a better approach to this.
Thank you for your time!

ideas for removing background fringes using opencv

basically i wrote a code that had two images. a reference img and a background img. So far i have successfully found the matching image by using feature recognition. Then i rotated it and resized it to look identical as the reference image. The only problem left is the fact that the image as some of the background image on the fringes of the object. This image has been appropriately cropped so i just need to work with the image below. The most obvious answer that first came to me was perhaps use a edge detection algorithm (canny) and use that to give me a clue on where the background may lie. However since the images itself could technically be anything i feel like there would be lots of noise and various unusual errors so if possible i would rather not want to take that path. I also saw the backgroundsubtraction MOG but it seemed like that works for videos and not for single stilled image. In case i was wrong i tried the following code but had 0 effect:
BackgroundSubtractorMOG bs_mog(3, 4, 0.8);
Mat foreground_mog;
bs_mog (cropped_img, foreground_mog, -1.0);
Perhaps i am doing it wrong. So my thought is other than edge detection and if backgroundsubtractorMOG is only for moving images are there any other ideas or options i can look into to remove the fringe background image (i want to turn it all into just white)
thank you in advance for your ideas and comments
EDIT:
well i unerstand the logic already posted by others but i am unsure what the best way to make a mask for this bottom image would be. It is important to note that the image can technically be anything. Not necessary round in shape. Also due to changes in the algorithm the shape must be resized after the object is separated from the background. This means i can't use my reference image to just make a mask and use that mask on this image due to the difference in size.
Segmentation could work. Try cvgrabCut() with a customized mask.
Set as background all pixels very close to borders. (red in image below)
Set as foreground the center area of your image. (green in image below)
Any intermediate pixels set them to probably foreground. (gray in image below)

Extract Rectangle From Contour OpenCV

after making some edge and corner detection and then find contours i have this output.
how i can crop this image and return only this rectangle using openCV
EDIT:
i tried cvBoundingRect and then setimageROI but the output image still having some background but i want the rectangle only
Thank You.
i hope you need the rectangle area you selected.
For this you need to make another grayscale image, let us call it 'mask'. Then draw the rectangle contour obtained on it and fill it with white (255,255,255). You will obtain an image like this ( all images hand-edited in paint program):
Now just have an bitwise_and operation on both the images. You will get result as this:
**NB:**Now if it is not the one you wanted, instead you wanted this kind of selection, You can find information about it in this SOF question (thanks to karl philip for the link) .
I guess Mustafa wants to get the box automatically? If not, please accept Abid's answer and ignore this one.
Otherwise:
As I don't know how far it should generalize, for this specific image, do hough transform, which gives you straight lines. However the line at the bottom can become false positive. But with some post processing, e.g. blur, dilate, you will be able to get rid of it. Or you could use the knowledge that the lines build a rectangle.