如何在图像中找到垂直线?(Opencv和Python)总垂直雪崩需要精细处理

2024-09-30 08:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好

我需要在图片中标记垂直线并找到数字。在下面的示例中,我无法准确标记。因此,我发现垂直线的数字错误。我应该将线画直,但我无法绘制 我哪里会错呢?

enter image description here

结果被发现(错误)print(len(contours)) 25 正确结果=(22)

import cv2
import numpy as np
from matplotlib import pyplot as plt
# Functions

def resizewithAspectRatio(img,width=None,height=None):
    return cv2.resize(img,(width,height),cv2.INTER_LINEAR)


#------------------------

img=resizewithAspectRatio(cv2.imread("1.jpg"),640,640)
gray_img=resizewithAspectRatio(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY),640,640) 

empty_img=np.zeros((640,640),np.uint8)+255
kernel = np.ones((5,5),np.uint8)
kernel_size = (3,3)
#Apply Filter

gray_img=cv2.medianBlur(gray_img,3)
gray_img = cv2.bilateralFilter(gray_img,9,75,75)


#--------------------------

threshold = cv2.adaptiveThreshold(gray_img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.THRESH_BINARY_INV,83,3)


Reverse_img=np.where(threshold==255, 0, 
         (np.where(threshold==0, 255, threshold)))



closing = cv2.morphologyEx(Reverse_img, cv2.MORPH_CLOSE, kernel,iterations=2)


edges = cv2.Canny(closing,50,150,apertureSize = 3)
linesP = cv2.HoughLinesP(edges, 1, np.pi / 180, 50, None, 1, 100)
    
if linesP is not None:
    for i in range(0, len(linesP)):
        l = linesP[i][0]
        cv2.line(empty_img, (l[0], l[2]), (l[3], l[4]), (0,0,255), 4, cv2.LINE_AA)





cv2.imshow("ıM",empty_img)


contours,hierarchy=cv2.findContours(empty_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(len(contours))



titles=['Org_Img','threshold','Reverse_img','closing','empty_img']
images=[img,threshold,Reverse_img,closing,empty_img]
for i in range(5):
    plt.subplot(3,3,i+1),plt.imshow(images[i] , 'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
    
plt.show()

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here


Tags: importnoneimgthresholdlennppltcv2
1条回答
网友
1楼 · 发布于 2024-09-30 08:19:18

请参阅我的other answer作进一步解释

Result

import numpy as np
import matplotlib.pyplot as plt
import cv2

# Read image
img = cv2.imread('input.jpg', 0)

# #            
# # Morphology
# #========================
# # Closing
# #            
closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (1, 7)))

# #            
# # Statistics
# #========================
dens = np.sum(img, axis=0)
mean = np.mean(dens)

#            
# Thresholding
#========================
thresh = 255 * np.ones_like(img)
k = 0.9
for idx, val in enumerate(dens):
    if val< k*mean:
        thresh[:,idx] = 0

thresh = 255 - thresh
contours,hierarchy=cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
count = len(contours)


#            
# plotting the results
#========================
plt.figure(num='{} Lines'.format(count))

plt.subplot(221)
plt.imshow(img, cmap='gray')
plt.title('Original')
plt.axis('off')

plt.subplot(223)
plt.imshow(thresh, cmap='gray')
plt.title('Thresholded')
plt.axis('off')

plt.subplot(224)
plt.imshow((thresh/255)*img, cmap='gray')
plt.title('Result')
plt.axis('off')

plt.subplot(222)
plt.hist(dens)
plt.axvline(dens.mean(), color='k', linestyle='dashed', linewidth=1)
plt.title('dens hist')

plt.show()

相关问题 更多 >

    热门问题