OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Mon, 01 Apr 2019 05:25:15 -0500how implement Sobel edge detection opencvhttp://answers.opencv.org/question/211010/how-implement-sobel-edge-detection-opencv/ I am trying to implement sobel edge detection from scratch but my output can't seem to match with OpenCV's sobel function. I performed correlation on the image with the sobel operator in both x and y directions and then computed gradient magnitude as square root of sum of squares of magnitudes in both x & y direction. I believe the problem is how I assign the threshold for edge detection.jamie andersonMon, 01 Apr 2019 05:25:15 -0500http://answers.opencv.org/question/211010/Why is general OpenCV so fast compared to a specialised implementationhttp://answers.opencv.org/question/201590/why-is-general-opencv-so-fast-compared-to-a-specialised-implementation/ I was looking through some old OpenCV university work, and cleaning it up so I could use it in the future.
For our work we had been asked to not use some OpenCV functions and instead to write our own, to gain a better understanding of how the functions are working. In one such case I had written my own function to take an image and find the "Polar Co-ordinates" of the image (according to the comments I made for myself). This was to get the derivatives of an image and calculate the directions of change so I would have a "gradient magnitude" and a "gradient angle" of the image. I also wanted to obtain the mean value in the "gradient magnitude" image, for processing that came later.
I wrote a single function to do all of the above with one full iteration of the original image, basically doing everything at the same time.
I found that in order to obtain the same result with OpenCV functions only would require 4 separate calls:
cv::Sobel(original_image, grad_mag_x, -1, 1, 0, 3);
cv::Sobel(original_image, grad_mag_y, -1, 0, 1, 3);
cv::cartToPolar(grad_mag_x, grad_mag_y, grad_mag_total, grad_angle);
auto mean = cv::mean(grad_mag_total)[0];
3 iterations of the original image are required, with one further iteration over two images simultaneously.
So I went ahead and timed my implementation against OpenCV's implementation. I found my code running an order of magnitude slower with no optimisation and when compiled for speed it was running only twice as slow.
**Why is this the case? Surely something specialised that runs through n by n data once would be faster than something running through an n by n set of data 4 times?**
What makes me the most curious is I would have thought that looping through an n x n image 3 times would upset the hardware cache and cause more frequent direct reads to memory, making the whole process orders of magnitude slower.
Of course, if you read all of this and think that my implementation really should be faster then I shall take a good hard look at what I wrote all those years ago and make it better.ErudorikuSun, 21 Oct 2018 11:39:40 -0500http://answers.opencv.org/question/201590/Sobel using dx=1, dy=0 gives exactly same output as dx=0, dy=1 (bug?)http://answers.opencv.org/question/123432/sobel-using-dx1-dy0-gives-exactly-same-output-as-dx0-dy1-bug/ Greetings,
this piece of code seems to be generating exactly same Sobel gradients for dx=1, dy=0 and dx=0, dy=1. This means I can't subtract gradY from gradX because that leaves me with a black image. Is this a bug in OpenCV?
Mat gradX = new Mat();
Mat gradY = new Mat();
Mat gray = new Mat();
Mat gradient = new Mat()
Imgproc.cvtColor( cameraSnapshotFrame, gray, COLOR_BGR2GRAY );
Imgproc.Sobel( gray, gradX, CV_32F, 1, 0, -1, 1.0, 0 );
Imgproc.Sobel( gray, gradY, CV_32F, 0, 1, -1, 1.0, 0 ); // for some reason this gives the same result as the line above
Core.subtract( gradX, gradY, gradient );
Core.convertScaleAbs( gradient, gradient ); // gradient is an empty black image
Using OpenCV for Android version 3.1.0. Please help.NTxCSat, 21 Jan 2017 06:17:56 -0600http://answers.opencv.org/question/123432/Implement angle constraint in the Sobel operatorhttp://answers.opencv.org/question/111413/implement-angle-constraint-in-the-sobel-operator/I have a couple of doubts related to edge detection in this question.
1) The code I have written below tries to show only those edges which obey a certain constraint of magnitude and direction.
The opencv function to display image displays only black when I use the numpy methods.
In the `show_angle` function when I implemented it using `for` loops and that displayed the image using `cv2.imshow`.
I then checked the ouput using numpy methods and my for loop using `np.array_equal` which returned `True`.
What might be the reason behind that?
2) I am not able to work the angle constraints, I will post a few images for different angle constraints.
import cv2
import numpy as np
import matplotlib.pyplot as plt
def show_image(name, img, waitkey=0):
cv2.namedWindow(name, 0)
cv2.imshow(name, img)
cv2.waitKey(waitkey)
cv2.destroyWindow(name)
img = cv2.imread('hex2.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
shape = img.shape
out_x = cv2.Sobel(img, cv2.CV_16S, 1, 0) # x gradient
out_y = cv2.Sobel(img, cv2.CV_16S, 0, 1) # y gradient
out_x = cv2.convertScaleAbs(out_x)
out_y = cv2.convertScaleAbs(out_y)
out_weight = cv2.addWeighted(out_x, 0.5, out_y, 0.5,0) # x and y weighted
def show_angle(out_weight, mag_final, dir_final, min_mag, theta_min, theta_max):
"""
Return points based on magnitude and angle constraints
"""
out_img = np.multiply(
(
(mag_final > min_mag) &
(dir_final > theta_min) &
(dir_final < theta_max)
).astype(int),
out_weight
)
return out_img
def mag_dir():
"""
Calculate gradient magnitude and direction matrix
"""
mag = np.sqrt(
np.add
(
np.square(out_x) , np.square(out_y)
)
)
dir = np.arctan2(out_y, out_x)
dir = np.multiply(dir, 180)
print np.min(dir) # 0
print np.max(dir) # 282
plt.hist(dir,8, (0,360))
plt.show()
return mag, dir
mag, dir = mag_dir()
out_img = show_angle(out_weight, mag, dir, 0, 90,120)
plt.imshow(out_img, cmap='gray')
plt.show()
Input image :
[![hexagon image][1]][1]
Image Histogram :
[![Image histogram for the hexagon.][2]][2]
Output for some constraints :
0 to 90 degrees
[![0 to 90 degrees][3]][3]
90 to 180 degrees
[![90 to 180 degrees][4]][4]
Thanks.
[1]: https://i.stack.imgur.com/vPVRZ.png
[2]: https://i.stack.imgur.com/O9iFP.png
[3]: https://i.stack.imgur.com/NSkYr.png
[4]: https://i.stack.imgur.com/YMWqe.pngsupertramp-sidTue, 08 Nov 2016 08:13:10 -0600http://answers.opencv.org/question/111413/Weird result while finding anglehttp://answers.opencv.org/question/234/weird-result-while-finding-angle/Hi
I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.
Then i applied Sobel derivative in x and y direction and to find the angle, i took **arctan(dy/dx)**. ( dx = dx+0.001 to avoid divide by zero)
**For vertical side, i got an angle of 0** which is true since dy=0 in vertical direction and dx is some high value.
**But for horizontal side also, i got zero.** I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and **dy = 0**. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?
I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. **But still, why dy = 0 on that side?**
Below is my dx image:
![image description](/upfiles/13419390596711553.png)
My dy image:
![image description](/upfiles/13419390788350963.png)
Abid Rahman KTue, 10 Jul 2012 11:52:57 -0500http://answers.opencv.org/question/234/