Convert the black and white image after canny edge detection to color image

import cv2
import sys
import numpy as np
from matplotlib import pyplot as plt
### First way of cropping and edge detection
img = cv2.imread("plug.jpeg")
if img is None:
    sys.exit("Could not show image")
rsz_img = cv2.resize(img,None, fx=0.25, fy=0.25)
rsz_blur = cv2.GaussianBlur(rsz_img,(3,3),0,0)
edges = cv2.Canny(rsz_blur,100,200)
pts = np.argwhere(edges>0)
y1,x1 = pts.min(axis=0)
y2,x2 = pts.max(axis=0)
## crop the region
filename = 'savedImage.jpg'
cropped = edges[y1:y2, x1:x2]
cv2.imwrite(filename, cropped)
plt.imshow(img ,cmap = 'jet')
plt.title('Original Image'), plt.xticks([]),
plt.imshow(cropped,cmap = '')
plt.title('Canny Edge Detection')
plt.xticks([]), plt.yticks([])

I am not sure how to convert this BW image to color image same as before? Thank you.

this is not possible.(what did you expect ?)

what you can do is: duplicate the single existing channel into a 3 channel image (but why ?)
what you cannot do is: “reconstruct” color from bw information

again, why do you want to do this ? please explain


Thank you for the suggestion. The reason is that I want to get the specific area from the image below. But sometimes the image will vary so I think of edge detection first to detect the area I want to crop and then crop the image. Pls see the attached photo below.

detect it, and then what?

I’ll go ahead and say your approach can’t work. discard it and let people suggest other approaches… which requires that you explain the goal. what you’ve explained is steps towards the goal, not the goal itself.

the goal is simple, to collect dataset.

giving an answer isn’t the same as answering.

For those who have similar issues like me, I have used template matching to see the object inside and crop that area after that.