简体   繁体   中英

How to recolor image based on edge detection(canny)

I have a script, which is using for recoloring room walls based on color similarity. But I need to recolor a wall based on edge detection.

import cv2
import numpy as np
import sys
from PIL import Image
import numpy as np
from hex_to_rgb import color

def recolor(file_path, celor, lower_color, upper_color):
    
    img = cv2.imread(file_path)
    res = img.copy()
    rgb = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    r2, g2, b2 = color(celor)

    mask = cv2.inRange(rgb, lower_color, upper_color)

    mask = mask/255
    mask = mask.astype(np.bool)
    res[:,:,:3][mask] = [b2, g2, r2] # opencv uses BGR
    im_rgb = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
    return im_rgb

file_path --> image

celor --> color, which you want to recolor

lower_color --> lower values of RGB

upper_color --> upper values of RGB

原始图像

基于颜色相似度的重新着色图像

I am using Sobel edge detection to solve this problem. I tried with Canny edge detection also but it didn't give good results.

After edge detection, I applied the threshold to the image and found contours in the image. The problem here is that I am coloring the contour with the maximum area in this case. You will have to figure out a way to choose the contour you want to color.

img = cv2.imread("colourWall.jpg")
cImg = img.copy()
img = cv2.blur(img, (5, 5))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

scale = 1
delta = 0
ddepth = cv.CV_16S

grad_x = cv.Sobel(gray, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)
grad_y = cv.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)

abs_grad_x = cv.convertScaleAbs(grad_x)
abs_grad_y = cv.convertScaleAbs(grad_y)

grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)

ret, thresh = cv2.threshold(grad, 10, 255, cv2.THRESH_BINARY_INV)

c, h = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

areas = [cv2.contourArea(c1) for c1 in c]
maxAreaIndex = areas.index(max(areas))

cv2.drawContours(cImg, c, maxAreaIndex, (255, 0, 0), -1)

plt.imshow(cImg)
plt.show()

Result:

输出图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM