So I'm trying to reproduce a cool filter I did a while back in C# (emgucv) in Python cv2. Despite my hopes it's not going very smoothly. The programs suppose to highlight edges and color them with a cool looking gradient.
The code in C#:
{
Image<Gray, byte> gray= imgColored.Convert<Gray, byte>();
Image<Gray, float> photo_dx = gray.Sobel(1, 0, 3);
Image<Gray, float> photo_dy = gray.Sobel(0, 1, 3);
Image<Gray, float> photo_grad = new Image<Gray, float>(gray.Size);
Image<Gray, float> photo_angle = new Image<Gray, float>(gray.Size);
CvInvoke.CartToPolar(photo_dx, photo_dy, photo_grad, photo_angle, true);
Image<Hsv, float> coloredEdges = gray.Convert<Hsv, float>();
for (int j = 0; j < coloredEdges.Cols; j++)
for (int i = 0; i < coloredEdges.Rows; i++)
{
Hsv pix = coloredEdges[i, j];
pix.Hue = photo_angle[i, j].Intensity;
pix.Satuation = 1;
pix.Value = photo_grad[i, j].Intensity;
coloredEdges[i, j] = pix;
}
coloredEdges.Save("test.jpg");
}
The code in Python:
def LSD_ify(image, mag, angle):
image = image = image.astype(np.float64)
height, width, depth = image.shape
for x in range(0, height):
for y in range(0, width):
image[x, y, 0] = angle[x, y]
image[x, y, 1] = 1
image[x, y, 2] = mag[x, y]
return image
def main():
image = plt.imread(str(sys.argv[1]))
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
g2bgr = cv.cvtColor(gray_image, cv.COLOR_GRAY2BGR) #cv2 cant convert gray to HSV directly, so i had to convert back to colored and finally to HSV
gx = cv.Sobel(gray_image, cv.CV_64F, 1, 0, ksize = 3)
gy = cv.Sobel(gray_image, cv.CV_64F, 0, 1, ksize = 3)
mag, angle = cv.cartToPolar(gx, gy, angleInDegrees = True)
hsv_image = cv.cvtColor(g2bgr, cv.COLOR_BGR2HSV)
lsd = LSD_ify(hsv_image, mag, angle)
cv.imwrite("test.jpg", lsd)
if __name__ == "__main__":
main()
The code is mostly identical(i think?), the results they produce however are different.
The input image:

Does anyone have any insight on what I'd have to do to get identical results in Python? I'm not sure how things work in the background in Python.




