pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何在 HoughLinesP 之后合并行?

How to merge lines after HoughLinesP?(如何在 HoughLinesP 之后合并行?)
本文介紹了如何在 HoughLinesP 之后合并行?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的任務是找到線(startX、startY、endX、endY)和矩形(4 線)的坐標.這是輸入文件:

我使用下一個代碼:

img = cv2.imread(image_src)灰色 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)ret, thresh1 = cv2.threshold(灰色,127,255,cv2.THRESH_BINARY)邊緣 = cv2.Canny(thresh1,50,150,apertureSize = 3)minLineLength = 100最大線間隙 = 10線 = cv2.HoughLinesP(edges,1,np.pi/180,10,minLineLength,maxLineGap)打印(長度(行))對于行中的行:cv2.line(img,(line[0][0],line[0][1]),(line[0][2],line[0][3]),(0,0,255),6)

我得到下一個結果:

從最后一張圖片中,您可以看到大量的小紅線.

問題:

  1. 合并小線條的最佳方法是什么?
  2. 為什么有很多HoughLinesP 檢測不到的小部分?

解決方案

我終于完成了流水線:

  1. 修正了不正確的參數(Dan 建議)
  2. 開發了我自己的合并線段"算法.

    還有 572 行.在我的合并線段"之后,我們只有 89 行

    My task is to find coordinates of lines (startX, startY, endX, endY) and rectangles (4 lines). Here is input file:

    I use the next code:

    img = cv2.imread(image_src)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
    
    edges = cv2.Canny(thresh1,50,150,apertureSize = 3)
    
    minLineLength = 100
    maxLineGap = 10
    lines = cv2.HoughLinesP(edges,1,np.pi/180,10,minLineLength,maxLineGap)
    print(len(lines))
    for line in lines:
        cv2.line(img,(line[0][0],line[0][1]),(line[0][2],line[0][3]),(0,0,255),6)
    

    I get the next results:

    From the last image you can see big amount of small red lines.

    Questions:

    1. What is the best way to merge small lines?
    2. Why there are a lot of small portions that are not detected by HoughLinesP?

    解決方案

    I have finally completed the pipeline:

    1. fixed incorrect parameters (as were suggested by Dan)
    2. developed my own 'merging line segments' algorithm. I had bad results when I implemented TAVARES and PADILHA algorithm (as were suggested by Andrew).
    3. I have skipped Canny and got better results (as were suggested by Alexander)

    Please find the code and results:

    def get_lines(lines_in):
        if cv2.__version__ < '3.0':
            return lines_in[0]
        return [l[0] for l in lines_in]
    
    
    def process_lines(image_src):
        img = mpimg.imread(image_src)
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
        ret, thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
    
        thresh1 = cv2.bitwise_not(thresh1)
    
        edges = cv2.Canny(thresh1, threshold1=50, threshold2=200, apertureSize = 3)
    
        lines = cv2.HoughLinesP(thresh1, rho=1, theta=np.pi/180, threshold=50,
                                minLineLength=50, maxLineGap=30)
    
        # l[0] - line; l[1] - angle
        for line in get_lines(lines):
            leftx, boty, rightx, topy = line
            cv2.line(img, (leftx, boty), (rightx,topy), (0,0,255), 6) 
    
        # merge lines
    
        #------------------
        # prepare
        _lines = []
        for _line in get_lines(lines):
            _lines.append([(_line[0], _line[1]),(_line[2], _line[3])])
    
        # sort
        _lines_x = []
        _lines_y = []
        for line_i in _lines:
            orientation_i = math.atan2((line_i[0][1]-line_i[1][1]),(line_i[0][0]-line_i[1][0]))
            if (abs(math.degrees(orientation_i)) > 45) and abs(math.degrees(orientation_i)) < (90+45):
                _lines_y.append(line_i)
            else:
                _lines_x.append(line_i)
    
        _lines_x = sorted(_lines_x, key=lambda _line: _line[0][0])
        _lines_y = sorted(_lines_y, key=lambda _line: _line[0][1])
    
        merged_lines_x = merge_lines_pipeline_2(_lines_x)
        merged_lines_y = merge_lines_pipeline_2(_lines_y)
    
        merged_lines_all = []
        merged_lines_all.extend(merged_lines_x)
        merged_lines_all.extend(merged_lines_y)
        print("process groups lines", len(_lines), len(merged_lines_all))
        img_merged_lines = mpimg.imread(image_src)
        for line in merged_lines_all:
            cv2.line(img_merged_lines, (line[0][0], line[0][1]), (line[1][0],line[1][1]), (0,0,255), 6)
    
    
        cv2.imwrite('prediction/lines_gray.jpg',gray)
        cv2.imwrite('prediction/lines_thresh.jpg',thresh1)
        cv2.imwrite('prediction/lines_edges.jpg',edges)
        cv2.imwrite('prediction/lines_lines.jpg',img)
        cv2.imwrite('prediction/merged_lines.jpg',img_merged_lines)
    
        return merged_lines_all
    
    def merge_lines_pipeline_2(lines):
        super_lines_final = []
        super_lines = []
        min_distance_to_merge = 30
        min_angle_to_merge = 30
    
        for line in lines:
            create_new_group = True
            group_updated = False
    
            for group in super_lines:
                for line2 in group:
                    if get_distance(line2, line) < min_distance_to_merge:
                        # check the angle between lines       
                        orientation_i = math.atan2((line[0][1]-line[1][1]),(line[0][0]-line[1][0]))
                        orientation_j = math.atan2((line2[0][1]-line2[1][1]),(line2[0][0]-line2[1][0]))
    
                        if int(abs(abs(math.degrees(orientation_i)) - abs(math.degrees(orientation_j)))) < min_angle_to_merge: 
                            #print("angles", orientation_i, orientation_j)
                            #print(int(abs(orientation_i - orientation_j)))
                            group.append(line)
    
                            create_new_group = False
                            group_updated = True
                            break
    
                if group_updated:
                    break
    
            if (create_new_group):
                new_group = []
                new_group.append(line)
    
                for idx, line2 in enumerate(lines):
                    # check the distance between lines
                    if get_distance(line2, line) < min_distance_to_merge:
                        # check the angle between lines       
                        orientation_i = math.atan2((line[0][1]-line[1][1]),(line[0][0]-line[1][0]))
                        orientation_j = math.atan2((line2[0][1]-line2[1][1]),(line2[0][0]-line2[1][0]))
    
                        if int(abs(abs(math.degrees(orientation_i)) - abs(math.degrees(orientation_j)))) < min_angle_to_merge: 
                            #print("angles", orientation_i, orientation_j)
                            #print(int(abs(orientation_i - orientation_j)))
    
                            new_group.append(line2)
    
                            # remove line from lines list
                            #lines[idx] = False
                # append new group
                super_lines.append(new_group)
    
    
        for group in super_lines:
            super_lines_final.append(merge_lines_segments1(group))
    
        return super_lines_final
    
    def merge_lines_segments1(lines, use_log=False):
        if(len(lines) == 1):
            return lines[0]
    
        line_i = lines[0]
    
        # orientation
        orientation_i = math.atan2((line_i[0][1]-line_i[1][1]),(line_i[0][0]-line_i[1][0]))
    
        points = []
        for line in lines:
            points.append(line[0])
            points.append(line[1])
    
        if (abs(math.degrees(orientation_i)) > 45) and abs(math.degrees(orientation_i)) < (90+45):
    
            #sort by y
            points = sorted(points, key=lambda point: point[1])
    
            if use_log:
                print("use y")
        else:
    
            #sort by x
            points = sorted(points, key=lambda point: point[0])
    
            if use_log:
                print("use x")
    
        return [points[0], points[len(points)-1]]
    
    # https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html
    # https://stackoverflow.com/questions/32702075/what-would-be-the-fastest-way-to-find-the-maximum-of-all-possible-distances-betw
    def lines_close(line1, line2):
        dist1 = math.hypot(line1[0][0] - line2[0][0], line1[0][0] - line2[0][1])
        dist2 = math.hypot(line1[0][2] - line2[0][0], line1[0][3] - line2[0][1])
        dist3 = math.hypot(line1[0][0] - line2[0][2], line1[0][0] - line2[0][3])
        dist4 = math.hypot(line1[0][2] - line2[0][2], line1[0][3] - line2[0][3])
    
        if (min(dist1,dist2,dist3,dist4) < 100):
            return True
        else:
            return False
    
    def lineMagnitude (x1, y1, x2, y2):
        lineMagnitude = math.sqrt(math.pow((x2 - x1), 2)+ math.pow((y2 - y1), 2))
        return lineMagnitude
    
    #Calc minimum distance from a point and a line segment (i.e. consecutive vertices in a polyline).
    # https://nodedangles.wordpress.com/2010/05/16/measuring-distance-from-a-point-to-a-line-segment/
    # http://paulbourke.net/geometry/pointlineplane/
    def DistancePointLine(px, py, x1, y1, x2, y2):
        #http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/source.vba
        LineMag = lineMagnitude(x1, y1, x2, y2)
    
        if LineMag < 0.00000001:
            DistancePointLine = 9999
            return DistancePointLine
    
        u1 = (((px - x1) * (x2 - x1)) + ((py - y1) * (y2 - y1)))
        u = u1 / (LineMag * LineMag)
    
        if (u < 0.00001) or (u > 1):
            #// closest point does not fall within the line segment, take the shorter distance
            #// to an endpoint
            ix = lineMagnitude(px, py, x1, y1)
            iy = lineMagnitude(px, py, x2, y2)
            if ix > iy:
                DistancePointLine = iy
            else:
                DistancePointLine = ix
        else:
            # Intersecting point is on the line, use the formula
            ix = x1 + u * (x2 - x1)
            iy = y1 + u * (y2 - y1)
            DistancePointLine = lineMagnitude(px, py, ix, iy)
    
        return DistancePointLine
    
    def get_distance(line1, line2):
        dist1 = DistancePointLine(line1[0][0], line1[0][1], 
                                  line2[0][0], line2[0][1], line2[1][0], line2[1][1])
        dist2 = DistancePointLine(line1[1][0], line1[1][1], 
                                  line2[0][0], line2[0][1], line2[1][0], line2[1][1])
        dist3 = DistancePointLine(line2[0][0], line2[0][1], 
                                  line1[0][0], line1[0][1], line1[1][0], line1[1][1])
        dist4 = DistancePointLine(line2[1][0], line2[1][1], 
                                  line1[0][0], line1[0][1], line1[1][0], line1[1][1])
    
    
        return min(dist1,dist2,dist3,dist4)
    

    There are still 572 lines. After my "merging line segments" we have only 89 lines

    這篇關于如何在 HoughLinesP 之后合并行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 复合肥,化肥厂,复合肥批发,化肥代理,复合肥品牌-红四方 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 制丸机,小型中药制丸机,全自动制丸机价格-甘肃恒跃制药设备有限公司 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 罐体电伴热工程-消防管道电伴热带厂家-山东沃安电气 | 办公室装修_上海办公室设计装修_时尚办公新主张-后街印象 | 升降机-高空作业车租赁-蜘蛛车-曲臂式伸缩臂剪叉式液压升降平台-脚手架-【普雷斯特公司厂家】 | 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 苏州注册公司_苏州代理记账_苏州工商注册_苏州代办公司-恒佳财税 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 头条搜索极速版下载安装免费新版,头条搜索极速版邀请码怎么填写? - 欧远全 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 算命免费_生辰八字_免费在线算命 - 卜算子算命网 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | CE认证_产品欧盟ROHS-REACH检测机构-商通检测| 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 神马影院-实时更新秒播 | 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 压片机_高速_单冲_双层_花篮式_多功能旋转压片机-上海天九压片机厂家 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 欧版反击式破碎机-欧版反击破-矿山石料破碎生产线-青州奥凯诺机械 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 酸度计_PH计_特斯拉计-西安云仪| 烟气在线监测系统_烟气在线监测仪_扬尘检测仪_空气质量监测站「山东风途物联网」 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 无机纤维喷涂棉-喷涂棉施工工程-山东华泉建筑工程有限公司▲ | 板式换网器_柱式换网器_自动换网器-郑州海科熔体泵有限公司 |