用python代码将图像中所有缺陷边缘进行显示
时间: 2023-03-13 10:06:09 浏览: 86
可以使用 OpenCV 和 PIL 库来处理图像,使用 Canny 边缘检测算法来检测图像中的所有边缘缺陷,然后在图像中显示出来。示例python代码如下:import cv2
import numpy as np
from PIL import Image# 读取图像
img = cv2.imread('image.jpg')
# 转换为灰度图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 使用Canny边缘检测
edges = cv2.Canny(gray,50,150,apertureSize = 3)
# 显示边缘图像
im_new = Image.fromarray(edges)
im_new.show()
相关问题
写一段python代码将图像中所有缺陷边缘进行用矩形框起来
# 导入所需的模块
import cv2
import numpy as np # 读取图像
img = cv2.imread('image.jpg') # 转换为灰度图像
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 检测边缘
edges = cv2.Canny(gray,50,150,apertureSize = 3) # 检测缺陷边缘
lines = cv2.HoughLines(edges,1,np.pi/180,200) # 遍历检测到的缺陷边缘
for line in lines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
# 用矩形框起来
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2) # 显示结果
cv2.imshow('Result Image',img)
cv2.waitKey(0)
写一段python代码将图像中最大的缺陷边缘进行用矩形框起来
import cv2
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edges = cv2.Canny(gray, 50, 150, apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength, maxLineGap)
for x1, y1, x2, y2 in lines[0]:
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow("Edges", edges)
cv2.imshow("Image", img)
cv2.waitKey(0)
阅读全文