提高图像质量以检测桌子上的盒子

时间:2019-05-22 07:13:31

标签: opencv opencv-contour

我正在尝试检测图像表中的框。如果表格以pdf格式存在,则该代码可以精美地检测所有盒子。但是,当从电话中单击图像并将其用于检测盒子时,它无法收集所有盒子。如果我尝试在openCV中使用equaliseHist,则图片质量会进一步下降,并且大多数图像会变暗。

代码如下:

img = cv2.imread(img_for_box_extraction_path, 0) 
(thresh, img_bin) = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)  # Thresholding the image
img_bin = 255-img_bin  # Invert the image
cv2.imwrite("Image_bin.jpg",img_bin)
kernel_length = np.array(img).shape[1]//40
verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))
hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# Morphological operation to detect verticle lines from an image
    img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)
    verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)
    cv2.imwrite("verticle_lines.jpg",verticle_lines_img)
# Morphological operation to detect horizontal lines from an image
    img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)
    horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3)
    cv2.imwrite("horizontal_lines.jpg",horizontal_lines_img)
# Weighting parameters, this will decide the quantity of an image to be added to make a new image.
    alpha = 0.5
    beta = 1.0 - alpha
    # This function helps to add two image with specific weight parameter to get a third image as summation of two image.
    img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)
    img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)
    (thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# For Debugging
    # Enable this line to see verticle and horizontal lines in the image which is used to find boxes
    cv2.imwrite("img_final_bin.jpg",img_final_bin)
    # Find contours for image, which will detect all the boxes
    contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # Sort all the contours by top to bottom.
    (contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")
    idx = 0
    for c in contours:
        # Returns the location and width,height for every contour
        x, y, w, h = cv2.boundingRect(c)
# If the box height is greater then 10, width is >10, then only save it as a box in "cropped/" folder.
        if (w > 10 and h > 10) and (w>=h):
            idx += 1
            new_img = img[y:y+h, x:x+w]
            #print(x,y,x+w,y+h)
            cv2.imwrite(cropped_dir_path+"{},{},{},{}".format(x,y,x+w,y+h) + '.png', new_img)
            #cv2.imwrite(cropped_dir_path+str(idx)+ '.png', new_img)

例如,这是为从电话中获取的桌子图像生成的骨架

通过手机快照生成的表格

Table generated by snap from from phone

这是从pdf表中生成的一个

从pdf表生成的表

Table generated from a pdf table

sort_contours方法对轮廓进行排序。 我也打算从相机点击的图片中收集所有盒子。如何平衡整个桌子上的光线?还是仅仅是照明问题?

0 个答案:

没有答案