OpenCV: How to detect thick red lines and their contours from the picture provided - c++

What solution you could suggest to detect thick red lines and their contours from the pictures attached? I use OpenCV.
Thank you in advance!

You could threshold the image to remove all the bright white/yellow pixels.
If the position is always approximately the same then the large remaning blob (in the red channel) on the right side of the image is your line.

Use adaptive threshold in red channel to binary the image
then use cvFindContours to fine each Couture.
To be sure that each contour is belong to one on thick lines you want you can use Bounding boxes. use cvMinAreaRect2 to find each contour Bounding box and check there width and height to near to size on red lines if there match there are lines if else there are noise.

Related

locate and display ROIs separately

I have a black Image with some clearly separated from each other white areas. This white image areas I would like to locate and display them separately, in the smallest possible rectangular shape.
Threshold the image cv::threshold
Find contours cv::findcontours
Use cv::BoundingBox on each contour
Use ROI concept to catch each rectangle (area)

Opencv height/width of part of image

I've an image like this one ![enter image description here][1]. The non-black part is expanded at each iteration. So, after a certain point, I need to enlarge the final image so the non-black one can fit in. For now, what I'm doing is to find the contour of the non-black image,find the bounding box of the contours and check the width/height of the box. At a first time it works, but after some iterations my program finds a bounding box of size 1 (it seems that it doesn't find any contour). What the problem could be?
Ps: the program is a mosaic from a video file, I followed the opencv tutorial for find homography and other stuff.
EDIT
Sorry but I had to remove images
Just a suggestion:
It's easier to simply iterate through each element in the matrix and record the coordinates of the uppermost, bottommost, leftmost and rightmost non-zero elements. These will be the four corners of your up-right bounding rectangle. Of course it is not necessarily the rectangle of the minimum area enclosing the non-zero pixels (not a rotated rectangle), but further can be used as a ROI.

Using Opencv how to detect a box in image while eliminating objects printed inside box?

I am trying to develop box sorting application in qt and using opencv. I want to measure width and length of box.
As shown in image above i want to detect only outermost lines (ie. box edges), which will give me width and length of box, regardless of whatever printed inside the box.
What i tried:
First i tried using Findcontours() and selected contour with max area, but the contour of outer edge is not enclosed(broken somewhere in canny output) many times and hence not get detected as a contour.
Hough line transform gives me too many lines, i dont know how to get only four lines am interested in out of that.
I tried my algorithm as,
Convert image to gray scale.
Take one column of image, compare every pixel with next successive pixel of that column, if difference in there value is greater than some threshold(say 100) that pixel belongs to edge, so store it in array. Do this for all columns and it will give upper line of box parallel to x axis.
Follow the same procedure, but from last column and last row (ie. from bottom to top), it will give lower line parallel to x axis.
Likewise find lines parallel to y axis as well. Now i have four arrays of points, one for each side.
Now this gives me good results if box is placed in such a way that its sides are exactly parallel to X and Y axis. If box is placed even slightly oriented in some direction, it gives me diagonal lines which is obvious as shown in below image.
As shown in image below i removed first 10 and last 10 points from all four arrays of points (which are responsible for drawing diagonal lines) and drew the lines, which is not going to work when box is tilted more and also measurements will go wrong.
Now my question is,
Is there any simpler way in opencv to get only outer edges(rectangle) of box and get there dimensions, ignoring anything printed on the box and oriented in whatever direction?
I am not necessarily asking to correct/improve my algorithm, but any suggestions on that also welcome. Sorry for such a big post.
I would suggest the following steps:
1: Make a mask image by using cv::inRange() (documentation) to select the background color. Then use cv::not() to invert this mask. This will give you only the box.
2: If you're not concerned about shadow, depth effects making your measurment inaccurate you can proceed right away with trying to use cv::findContours() again. You select the biggest contour and store it's cv::rotatedRect.
3: This cv::rotatedRect will give you a rotatedRect.size that defines the width en the height of your box in pixels
Since the box is placed in a contrasting background, you should be able to use Otsu thresholding.
threshold the image (use Otsu method)
filter out any stray pixels that are outside the box region (let's hope you don't get many such pixels and can easily remove them with a median or a morphological filter)
find contours
combine all contour points and get their convex hull (idea here is to find the convex region that bounds all these contours in the box region regardless of their connectivity)
apply a polygon approximation (approxPolyDP) to this convex hull and check if you get a quadrangle
if there are no perspective distortions, you should get a rectangle, otherwise you will have to correct it
if you get a rectangle, you have its dimensions. You can also find the minimum area rectangle (minAreaRect) of the convexhull, which should directly give you a RotatedRect

Extending a contour in OpenCv

i have several contours that consist of several black regions in my image. Directly adjacent to these black regions are some brighter regions that do not belong to my contours. I want to add these brighter regions to my black region and therefor extend my contour in OpenCv.
Is there a convenient way to extend a contour? I thought about looking at intensity change from my gradient-image created with cv::Sobel and extend until the gradient changes again, meaning the intensity of pixel is going back to the neither black nor bright regions of the image.
Thanks!
Here are example images. The first picture shows the raw Image, the second the extracted Contour using Canny & findContours, the last one the Sobel-Gradient intensity Image of the same area.
I want to include the bright boundaries in the first image to the Contour.
Update: Now i've used some morphological operations on the Sobelgradients and added a contour around them (see Image below). Next step could be to find the adjacent pair of purple & red contours, but it seems very much like a waste of procession time to actually have to search for directly adjacent contours. Any better ideas?
Update 2: My solution for now is to search for morphed gradient (red) contours in a bounding box around my (purple) contours and pick the one with correct orientation & size. This works for gradient contours where the morphological operation closes the "rise" and "fall" gradient areas like in Figure 3. But it is still a bad solution for cases in which the lighted area is wider then in the image above. Any idea is still very much appreciated, thanks!
What you're trying to do is find two different features and merge them. It's not terribly difficult but you have to use multiple copies of the image to make it happen.
Make one copy, and threshold it for the dark portion
Make another copy and threshold it for the light portion
Merge both thresholded images into a new image
Apply a morphological operation like opening or closing (depending on how you threshold) This will connect nearby components
Find contours in the resultant image
Use those contours on your original image. This will work since all the images are the same size and all based off of the original.

How to remove part of image containing text in opencv

What method can I use in opencv to remove the black section containing text at the bottom of the image?
Any help appreciated
if Blender's suggestion does not work in your case, you can:
Threshold the image so that all pixels higher than 0 will become 255.
Find contours.
Calculate a bounding rectangle for each contour.
Define the largest bounding rectangle as the region to be kept (the other ones are just the letters). You can then simply use the found rectangle as a ROI.
Good luck,