OpenCV Function to draw Gradient Orientation Arrows - c++

Does opencv have a function that can visually depict the gradient directions of an image?
Ie, a function that draws arrows over the top to visually show which direction the gradient slope runs? Or maybe colourise the image where red = angle 0 and so on?
Below is an example of what I mean when I say "visually depict the gradient directions of an image":
I know other people have done this but I don't want to reinvent the wheel if there is an OpenCV function or publically available function out there.

No, you need to implement it by yourself.

Related

How do I make a 2 point corner with a rectangle and keep the right thickness?

I'm trying to draw a dialog box with sfml and I'm using a VertexArray with quads. I want it to have a corner with 2 points like in this image:
Could someone help me with the math on how to get the length of A? Or maybe someone has a suggestion to do it in a different way in sfml?
What you are seeking is called the miter angle. And the miter angle is just half of the corner angle.
See this interactive diagram (at Geogebra)
By trigonometry, the ratio A/Th = TAN(angle/2).
A = Th * TAN(22.5)

Best way to draw boxes around pure red areas of an image?

I have an image with various odd shapes (such as circles and squares) which are coloured pure red (rgb(255, 0, 0) exactly). I want to draw boxes around these shapes, but to do that I need the coordinates of each corner from each box. This is the part I am having difficulty with.
I basically want to go from this:
To this:
I have tried many different ways to achieve this, including parsing the y-axis until I find a shape and boxing it that way, starting from the very corners of the image and moving towards the middle (both methods of which don't work well for multiple shapes) and using external packages such as an OpenCV.
I could use OpenCV to achieve this, but given the constraints I was hoping there was a way to do it which doesn't require an external package.
Can anyone with a bit more machine vision experience point me in the right direction please?
First, use the Hoshen-Kopelman algorithm to determine the connected clusters of pixels with the given criteria (being red), then all you have to do is find their min/max regions (on x and y axes) to wrap them with a rectangle.

OpenCV closing the arc of a semi circle shaped image

I have a semi circle shaped image that I am trying to make into a 120 degree fan shape. The image needs to be squished together between the two edges coming closer together.
After a lot of searching I've tried both affine and perspective transforms. Neither seemed to give me the results I was looking for.
I am using OpenCV2 and C++
How can I achieve this effect?
Edit:
Currently I map a rectangular image onto a semi circle. So, it would also be acceptable to map directly onto a 120 degree fan shape. Would that be a better approach? and if so how might I accomplish that?

Detect ball/circle in OpenCV (C++)

I am trying to detect a ball in an filtered image.
In this image I've already removed the stuff that can't be part of the object.
Of course I tried the HoughCircle function, but I did not get the expected output.
Either it didn't find the ball or there were too many circles detected.
The problem is that the ball isn't completly round.
Screenshots:
I had the idea that it could work, if I identify single objects, calculate their center and check whether the radius is about the same in different directions.
But it would be nice if it detect the ball also if he isn't completely visible.
And with that method I can't detect semi-circles or something like that.
EDIT: These images are from a video stream (real time).
What other method could I try?
Looks like you've used difference imaging or something similar to obtain the images you have..? Instead of looking for circles, look for a more generic loop. Suggestions:
Separate all connected components.
For every connected component -
Walk around the contour and collect all contour pixels in a list
Suggestion 1: Use least squares to fit an ellipse to the contour points
Suggestion 2: Study the curvature of every contour pixel and check if it fits a circle or ellipse. This check may be done by computing a histogram of edge orientations for the contour pixels, or by checking the gradients of orienations from contour pixel to contour pixel. In the second case, for a circle or ellipse, the gradients should be almost uniform (ask me if this isn't very clear).
Apply constraints on perimeter, area, lengths of major and minor axes, etc. of the ellipse or loop. Collect these properties as features.
You can either use hard-coded heuristics/thresholds to classify a set of features as ball/non-ball, or use a machine learning algorithm. I would first keep it simple and simply use thresholds obtained after studying some images.
Hope this helps.

Cocos2d - hiding, clipping or masking sprite like in game Candy Crush

I have a problem and I saw it also in the game Candy Crush Saga, where they successfully dealt with it. I would like the sprite to show only when it is in the board (see image link below). The board can be of different shapes, like the levels in the mentioned game.
Has anyone some ideas how can this be achieved with Cocos2d?
I will be very glad if someone has some tips.
Thank you in advance.
image link: http://www.android-games.fr/public/Candy-Crush-Saga/candy-crush-saga-bonus.jpg
In Cocos2d you can render sprites at different z levels. Images at a lower z-level will be drawn first by the graphic card and the images (sprites) with a higher z-value will be drawn later. Hence if an image (say A) is at the same position of the other but has a higher z-value you will see only the pixels of image A where the two images intersect.
Cocos2d uses also layers so you can decide to add Sprites to a layer and set the layer to a specific z value. I expect that they used a layer for the board (say at z=1) with a PNG image containing transparent bits in the area where you can see the sprites, and a second layer at z=0 for the sprites. In this way you can only see the sprites when they are in the transparent area.
Does this help?
I found out Cocos2d has a class CCClippingNode which does exatclly what I wanted. First I thought it can clip only rectangular areas, but after some research I found it can clip also paths.