#canny轮廓提取 edge = cv.Canny(opening, 50, 160, color1=(255, 255, 255), color2=(0, 0, 255))#提取轮廓50,160 io.imshow(edge,cmap='gray', vmin=0, vmax=255, origin='upper') io.show()这段代码我如何修改可以使canny检测后的图背景与线段颜色区分明显
时间: 2024-01-12 17:05:20 浏览: 22
你可以通过修改Canny函数的输出,使其输出的轮廓线条的颜色与背景颜色不同,从而可以更清晰地区分。具体来说,可以使用OpenCV库中的cv2.cvtColor()函数将Canny函数的输出从灰度图像转换为彩色图像,然后再使用cv2.bitwise_not()函数对图像进行反转,使背景变成白色,线条变成黑色,从而实现颜色区分明显的效果。代码如下:
```
edge = cv.Canny(opening, 50, 160)
edge = cv.cvtColor(edge, cv.COLOR_GRAY2BGR)
edge = cv.bitwise_not(edge)
io.imshow(edge)
io.show()
```
其中,cv.COLOR_GRAY2BGR是将灰度图像转换为彩色图像的参数。cv.bitwise_not()函数用于对图像进行反转。
相关问题
mask = None if self.training else (1 - mask / 255).astype(np.bool) # canny if self.edge == 1: # no edge if sigma == -1: return np.zeros(img.shape).astype(np.float) # random sigma if sigma == 0: sigma = random.randint(1, 4) return canny(img, sigma=sigma, mask=mask).astype(np.float)这段代码的含义,逐个函数解释
这段代码是一个函数,其目的是生成图像的边缘特征。下面逐个解释各个函数:
1. `mask = None if self.training else (1 - mask / 255).astype(np.bool)`:这一行根据是否处于训练模式来确定是否需要使用掩膜。如果在训练模式下,就不使用掩膜,否则将掩膜转换为二值数组,并将其反转。
2. `if self.edge == 1:`:这一行检查是否需要生成边缘特征。如果`self.edge`参数为1,则需要生成边缘特征。
3. `if sigma == -1:`:这一行检查是否需要生成随机sigma值。如果`sigma`参数为-1,则需要生成一个随机sigma值。
4. `if sigma == 0:`:这一行检查是否需要生成固定的sigma值。如果`sigma`参数为0,则需要生成一个介于1和4之间的随机sigma值。
5. `return canny(img, sigma=sigma, mask=mask).astype(np.float)`:这一行调用了`canny`函数生成图像的边缘特征,并将结果转换为浮点数类型。
综上所述,这段代码的作用是生成图像的边缘特征。如果需要生成随机的sigma值,则会在1到4之间随机生成一个值。如果需要使用掩膜,则将其转换为二值数组,并将其反转。最后,调用`canny`函数生成边缘特征,并将结果转换为浮点数类型。
import cv2 import numpy as np # 加载图像 img = cv2.imread('color_blind_road_2.png') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 直线检测 edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=100, maxLineGap=10) for line in lines: x1, y1, x2, y2 = line[0] cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) # 阈值分割 ret, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) thresh = cv2.medianBlur(thresh, 5) # 彩色连续性空间分割 hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) mask = cv2.inRange(h, 0, 20) | cv2.inRange(h, 160, 180) mask = cv2.bitwise_and(thresh, mask) # 纹理分割 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) texture = cv2.Canny(gray, 100, 200, apertureSize=3) texture = cv2.dilate(texture, None, iterations=3) texture = cv2.erode(texture, None, iterations=3) texture = cv2.bitwise_and(thresh, texture) # 显示结果 cv2.imshow('img', img) cv2.imshow('thresh', thresh) cv2.imshow('color', mask) cv2.imshow('texture', texture) cv2.waitKey(0) cv2.destroyAllWindows()改进代码
以下是对代码的一些改进建议:
1. 在 import 语句之间添加空格,使代码更易于阅读。
2. 使用函数封装代码,将不同的操作分别放入不同的函数中,可以使代码更加清晰和易于维护。
3. 添加异常处理,以避免因为文件路径或读取失败而导致程序崩溃。
4. 将一些常量或者需要调整的参数提取出来,以方便后续调整和修改。
5. 添加注释,以便代码的阅读和理解。
6. 使用 with 语句释放资源。
改进后的代码如下:
```python
import cv2
import numpy as np
def load_image(path):
"""加载图片"""
try:
img = cv2.imread(path)
return img
except Exception as e:
print(e)
return None
def gray_transform(img):
"""灰度转换"""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return gray
def edge_detection(img, threshold1=50, threshold2=150, apertureSize=3):
"""边缘检测"""
edges = cv2.Canny(img, threshold1, threshold2, apertureSize=apertureSize)
return edges
def line_detection(img, edges, threshold=100, minLineLength=100, maxLineGap=10):
"""直线检测"""
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=threshold, minLineLength=minLineLength, maxLineGap=maxLineGap)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
return img
def threshold_segmentation(img, threshold=150):
"""阈值分割"""
ret, thresh = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
thresh = cv2.medianBlur(thresh, 5)
return thresh
def hsv_segmentation(img, lower_range, upper_range):
"""HSV颜色空间分割"""
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_range, upper_range)
return mask
def color_segmentation(img, thresh, lower_range1=(0, 100, 100), upper_range1=(20, 255, 255), lower_range2=(160, 100, 100), upper_range2=(180, 255, 255)):
"""颜色分割"""
mask1 = hsv_segmentation(img, lower_range1, upper_range1)
mask2 = hsv_segmentation(img, lower_range2, upper_range2)
mask = cv2.bitwise_or(mask1, mask2)
mask = cv2.bitwise_and(thresh, mask)
return mask
def texture_segmentation(img, thresh, threshold1=100, threshold2=200, iterations=3):
"""纹理分割"""
gray = gray_transform(img)
texture = cv2.Canny(gray, threshold1, threshold2, apertureSize=3)
texture = cv2.dilate(texture, None, iterations=iterations)
texture = cv2.erode(texture, None, iterations=iterations)
texture = cv2.bitwise_and(thresh, texture)
return texture
def show_image(img, winname='image'):
"""显示图片"""
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
# 加载图片
img = load_image('color_blind_road_2.png')
if img is None:
exit()
# 灰度转换
gray = gray_transform(img)
# 边缘检测
edges = edge_detection(gray)
# 直线检测
img = line_detection(img, edges)
# 阈值分割
thresh = threshold_segmentation(gray)
# 颜色分割
mask = color_segmentation(img, thresh)
# 纹理分割
texture = texture_segmentation(img, thresh)
# 显示结果
show_image(img, 'img')
show_image(thresh, 'thresh')
show_image(mask, 'color')
show_image(texture, 'texture')
```