如何使用 Python 从原始图像中删除所有检测到的线条?
How to remove all the detected lines from the original image using Python?
我正在尝试删除图像中存在的所有线条。
我能够检测到线条,但是当我尝试删除线条时,最终图像中仍然只有几条细线。我已经使用 cv2.getStructuringElement
来获得水平线和垂直线。在某些情况下,最终图像变得完全扭曲,我无法前进
图片来自google
res = verticle_lines_img + horizontal_lines_img
res = cv2.bitwise_not(res)
fin=cv2.bitwise_or(img_bin, res,mask =cv2.bitwise_not(res))
fin= cv2.bitwise_not(fin)
exp =255-res
final = cv2.bitwise_and(exp,img_bin)
final = cv2.bitwise_not(final)
exp = ~exp
finalised = cv2.bitwise_and(img_bin,final)
finalised = cv2.bitwise_not(finalised)
请帮忙!谢谢
这是一个方法
- 将图像转换为灰度
- 大津获取二值图像的阈值
- 删除水平线
- 删除垂直线
转成灰度后,我们Otsu的阈值得到二值图像
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
从这里我们构建了一个特殊的水平内核来检测水平线。一旦检测到线条,我们填充线条以有效去除线条
# Remove horizontal
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
同样,为了去除垂直线,我们构建了一个特殊的垂直内核
# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
这是检测到的绿色线条
结果
您可以通过调整内核大小来微调结果。例如,将 (10,1)
更改为 (15,1)
将加强线检测,而将其降低到 (5,1)
将放松检测
完整代码
import cv2
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove horizontal
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
我正在尝试删除图像中存在的所有线条。
我能够检测到线条,但是当我尝试删除线条时,最终图像中仍然只有几条细线。我已经使用 cv2.getStructuringElement
来获得水平线和垂直线。在某些情况下,最终图像变得完全扭曲,我无法前进
图片来自google
res = verticle_lines_img + horizontal_lines_img
res = cv2.bitwise_not(res)
fin=cv2.bitwise_or(img_bin, res,mask =cv2.bitwise_not(res))
fin= cv2.bitwise_not(fin)
exp =255-res
final = cv2.bitwise_and(exp,img_bin)
final = cv2.bitwise_not(final)
exp = ~exp
finalised = cv2.bitwise_and(img_bin,final)
finalised = cv2.bitwise_not(finalised)
请帮忙!谢谢
这是一个方法
- 将图像转换为灰度
- 大津获取二值图像的阈值
- 删除水平线
- 删除垂直线
转成灰度后,我们Otsu的阈值得到二值图像
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
从这里我们构建了一个特殊的水平内核来检测水平线。一旦检测到线条,我们填充线条以有效去除线条
# Remove horizontal
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
同样,为了去除垂直线,我们构建了一个特殊的垂直内核
# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
这是检测到的绿色线条
结果
您可以通过调整内核大小来微调结果。例如,将 (10,1)
更改为 (15,1)
将加强线检测,而将其降低到 (5,1)
将放松检测
完整代码
import cv2
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove horizontal
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (255,255,255), 2)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()