久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從opencv中的數獨中提取框

Extract boxes from sudoku in opencv(從opencv中的數獨中提取框)
本文介紹了從opencv中的數獨中提取框的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經使用 opencv 將數獨圖像轉換為數獨網格

現在我想從圖像中提取每個框,最好的方法是什么?

據我所知,我正在嘗試找到線的交點以找到每個框的角

類 SudokuSolverPlay:def __init__(自我,圖像):def __preProcess(self, img):""""返回灰度圖像""""def __maskSudoku(self, img):""""返回蒙版圖像""""def __dectactEdge(self, img):""""返回數獨格子""""def drawLines(src, dest, 迭代=1):minLineLength = 100src = cv2.convertScaleAbs(src)對于 _ 在范圍內(迭代):線 = cv2.HoughLinesP(image=src, rho=1, theta=np.pi/180,閾值=100,行=np.array([]),minLineLength=minLineLength, maxLineGap=100)a, b, c = 線條.形狀對于范圍內的 i (a):x1, y1, x2, y2 = 線[i][0][0], 線[i][0][1], 線[i][0][2], 線[i][0][3]cv2.line(dest, (x1, y1), (x2, y2),255, 1, cv2.LINE_AA)src = cv2.convertScaleAbs(dest)def findVerticalLines(img):imgX = cv2.GaussianBlur(img, (5, 5), 0)kernelx = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 10))imgY = cv2.Sobel(img, cv2.CV_64F, 1, 0)imgY = cv2.convertScaleAbs(imgY)cv2.normalize(imgY, imgY, 0, 255, cv2.NORM_MINMAX)imgY = cv2.morphologyEx(imgY, cv2.MORPH_CLOSE, kernelx, 迭代次數=1)返回 imgYdef findHorizo??ntalLines(img):""與上面相同,只是參數不同""img1 = np.zeros(img.shape)邊緣 = cv2.Canny(img, 50, 150, 孔徑 = 3)拉普拉斯算子 = cv2.拉普拉斯算子(邊,cv2.CV_64F)drawLines(拉普拉斯算子,img1,迭代=1)sby = findVerticalLines(img1)sbx = findHorizo??ntalLines(img1)返回圖片1def 解決數獨(自我):灰色 = self.__preProcess(self.__originalImg)蒙面 = self.__maskSudoku(灰色)網格 = self.__dectactGrid(蒙面)如果 __name__ == '__main__':colorImg = cv2.imread('sudoku1.jpg')求解器 = SudokuSolverPlay(colorImg)求解器.solveSudoku()

這里的 findVerticalLines()findHorizo??ntalLines() 無法正確識別水平線和垂直線

  • 假設最小盒子尺寸為 20*20

    line_min_width = 20

    尋找水平線

    kernal_h = np.ones((1,line_min_width), np.uint8)img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_h)

    尋找垂直線

    kernal_v = np.ones((line_min_width,1), np.uint8)img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_v)

    合并并添加膨脹層以縮小小間隙

    img_bin_final=img_bin_h|img_bin_vfinal_kernel = np.ones((3,3), np.uint8)img_bin_final=cv2.dilate(img_bin_final,final_kernel,iterations=1)

    應用連通分量分析

    ret, labels, stats,centroids = cv2.connectedComponentsWithStats(~img_bin_final, connected=8, ltype=cv2.CV_32S)

    可視化連接組件圖像

    如您所見,我們檢測到一些文本也是框,我們可以通過簡單的過濾條件輕松刪除它們,這里我過濾的區域應至少為 1000 像素條件.

    在檢測到的盒子上繪制矩形.

    ### 1 和 0 以及我們不需要的背景和殘差連接組件對于 stats[2:] 中的 x,y,w,h,area:# cv2.putText(image,'box',(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX, 1.0,(0,255,0), 2)如果面積>1000:cv2.rectangle(圖像,(x,y),(x+w,y+h),(0,255,0),2)

    最終輸出圖片

    此答案基于我使用 OpenCV 在圖像中查找復選框/表格的解決方案.您可以在我的 博客中找到詳細說明走向數據科學.希望這將使您更接近解決方案.

    編碼愉快 :)

    -- 編輯 1

    進行連接組件可視化的代碼

    def imshow_components(labels):### 創建一個 hsv 圖像,每個標簽都有一個唯一的色調值label_hue = np.uint8(179*labels/np.max(labels))###使飽和度和音量為255empty_channel = 255*np.ones_like(label_hue)label_img = cv2.merge([label_hue, empty_channel, empty_channel])### 將 hsv 圖像轉換為 BGR 圖像標記的_img = cv2.cvtColor(標記的_img,cv2.COLOR_HSV2BGR)標記的_img[label_hue==0] = 0### 返回用于可視化連接組件的彩色圖像返回labeled_img

    i have converted sudoku image into sudoku grid using opencv

    now i want to extract each box from image what is best way to do this?

    as per my knowledge i am trying to find intersection points of lines to find corner of each box

    class SudokuSolverPlay:
          def __init__(self, image):
             
          def __preProcess(self, img):
            """return grayscale image"""
          def __maskSudoku(self, img):
            """return masked image"""
          def __dectactEdge(self, img):
            """return sudoku grid"""
            def drawLines(src, dest, iteration=1):
                minLineLength = 100
                src = cv2.convertScaleAbs(src)
                for _ in range(iteration):
                    lines = cv2.HoughLinesP(image=src, rho=1, theta=np.pi / 180,
                                        threshold=100, lines=np.array([]),
                                        minLineLength=minLineLength, maxLineGap=100)
                    a, b, c = lines.shape
                    for i in range(a):
                        x1, y1, x2, y2 = lines[i][0][0], lines[i][0][1], lines[i][0][2], lines[i][0][3]
                        cv2.line(dest, (x1, y1), (x2, y2),255, 1, cv2.LINE_AA)
                    src = cv2.convertScaleAbs(dest)
    
            def findVerticalLines(img):
                imgX = cv2.GaussianBlur(img, (5, 5), 0)
                kernelx = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 10))
                imgY = cv2.Sobel(img, cv2.CV_64F, 1, 0)
                imgY = cv2.convertScaleAbs(imgY)
                cv2.normalize(imgY, imgY, 0, 255, cv2.NORM_MINMAX)
                imgY = cv2.morphologyEx(imgY, cv2.MORPH_CLOSE, kernelx, iterations=1)
                return imgY
    
            def findHorizontalLines(img):
                """same as above only args different"""
    
            img1 = np.zeros(img.shape)
    
            edges = cv2.Canny(img, 50, 150, apertureSize=3)
            laplacian = cv2.Laplacian(edges, cv2.CV_64F)
            drawLines(laplacian, img1, iteration=1)
    
            sby = findVerticalLines(img1)
            sbx = findHorizontalLines(img1)
            return img1
          def solveSudoku(self):
              gray = self.__preProcess(self.__originalImg)
              masked = self.__maskSudoku(gray)
              grid = self.__dectactGrid(masked)
                       
    
    if __name__ == '__main__':
        colorImg = cv2.imread('sudoku1.jpg')
        solver = SudokuSolverPlay(colorImg)
        solver.solveSudoku()
    

    here findVerticalLines() and findHorizontalLines() are not able to dictect horizontal and vertical lines properly

    • original image
    • masked image
    • Canny edge dictation
    • hough line transform
    • horizontal lines
    • Vertical Lines

    解決方案

    One way to solve is to do a morphological operation to find vertical and horizontal lines from the canny edge image, then do a connected component analysis to find the boxes. I have done a sample version below. You can finetune it further to make it better. I started with the masked image as input.

    ### reading input image
    gray_scale=cv2.imread('masked_image.jpg',0)
    

    Performing canny edge detection and adding a dilation layer

    img_bin = cv2.Canny(gray_scale,50,110)
    dil_kernel = np.ones((3,3), np.uint8)
    img_bin=cv2.dilate(img_bin,dil_kernel,iterations=1)
    

    Now, the dilated binary image looks like this.

    assuming minimum box size would be 20*20

    line_min_width = 20
    

    finding horizontal lines

    kernal_h = np.ones((1,line_min_width), np.uint8)
    img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_h)
    

    finding vertical lines

    kernal_v = np.ones((line_min_width,1), np.uint8)
    img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_v)
    

    merging and adding a dilation layer to close small gaps

    img_bin_final=img_bin_h|img_bin_v
    final_kernel = np.ones((3,3), np.uint8)
    img_bin_final=cv2.dilate(img_bin_final,final_kernel,iterations=1)
    

    applying connected component analysis

    ret, labels, stats,centroids = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)
    

    visualising Connected component image

    AS you can see, we have detected some text also as boxes, we can easily remove them with simple filter conditions, Here I'm filtering with the area should be minimum 1000 pixels condition.

    drawing rectangles on the detected boxes.

    ### 1 and 0 and the background and residue connected components whihc we do not require
    for x,y,w,h,area in stats[2:]:
    #     cv2.putText(image,'box',(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX, 1.0,(0,255,0), 2)
        if area>1000:
            cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
    

    final output image

    This answer is based on my solution to find checkboxes/tables in an image using OpenCV. You can find a detailed explanation in my blog at Towards Data Science. Hope this will take you closer to a solution.

    Happy coding :)

    -- edit 1

    code to do connected component visualisation

    def imshow_components(labels):
        ### creating a hsv image, with a unique hue value for each label
        label_hue = np.uint8(179*labels/np.max(labels))
        ### making saturation and volume to be 255
        empty_channel = 255*np.ones_like(label_hue)
        labeled_img = cv2.merge([label_hue, empty_channel, empty_channel])
        ### converting the hsv image to BGR image
        labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
        labeled_img[label_hue==0] = 0
        ### returning the color image for visualising Connected Componenets
        return labeled_img
    

    這篇關于從opencv中的數獨中提取框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美亚洲国产一区二区三区 | 久久精品成人一区 | av在线免费观看网站 | 91成人小视频 | 日韩三级在线 | 精品综合在线 | 日韩区 | 欧美性受xxxx白人性爽 | 国产一区二区三区免费观看视频 | 成人日韩| 国产精品毛片无码 | 久久高清免费视频 | 国产欧美一区二区三区在线看 | 免费看av大片 | 人人艹人人爽 | 国产成人久久精品一区二区三区 | 欧美精品首页 | 午夜视频在线观看视频 | 黄色一级毛片 | 国产69久久精品成人看动漫 | 在线免费观看毛片 | 色婷婷精品国产一区二区三区 | 午夜专区 | 激情黄色在线观看 | 国产二区在线播放 | 日韩h | 中文字幕国产视频 | 婷婷色在线播放 | 久久伊人影院 | 99视频 | 中文字幕人成人 | 中文字幕国产高清 | 日日爱夜夜操 | 亚洲成人三级 | 成年人国产在线观看 | 国产片一区二区三区 | 亚洲三区在线观看 | 大香在线伊779| 国产高清免费视频 | 精品一区二区在线看 | 精品中文视频 |