1 | initial version |
So, you want to set all edges, which you got from Canny, to white in your original image. Here you can make use of bitwise_or
(cvtColor only converts your color space):
Input image:
Canny output:
Bitwise or of the input image with Canny:
And here the code (I just used Python, however it should work similarly in C++ and should also work similarly in Java):
import cv2, numpy as np
img = cv2.imread('cat.jpg')
# it seems canny can only deal with 1 channel 8-bit images
# --> let's take the mean and convert to 8bit (could also use cvtColor instead)
canny = cv2.Canny(np.mean(img, axis=2).astype(np.uint8), 50, 200)
cv2.imwrite('canny.png', canny)
# for bitwise_or the shape needs to be the same, so we need to add an axis,
# since our input image has 3 axis while the canny output image has only one 2 axis
out = np.bitwise_or(img, canny[:,:,np.newaxis])
cv2.imwrite('out.png', out)
I used here numpys' bitwise_or function, but it exists also directly in OpenCV, see: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=bitwise_or#cv2.bitwise_or
Alternative 1: Another way would be to create a new mat as large as the input image, set everything to 255. Invert the canny output image (bitwise_not) and use this as mask for copyTo, i.e. img.copyTo(white_image, canny_invert);
Alternative 2: You can also do everything by hand, i.e. you could also just loop over the image and make a look up in your mask and set the image accordingly.