I get this error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function ‘contourArea’

when I run the ros2 run self_driving_car_pkg computer_vision_node

this I get this error from termiinal =

    area = cv2.contourArea(cnt)
cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'

I didn’t open my frame and I couldn’t see my lanes on the track. But before the lane detection it was working

I don’t know but it can be because of the opencv version at firs I had 4.10.X then I tried lower version now it is 3.4.15 .So is the findContours suitable for this version this is my data_extraction code

import cv2
import numpy as np

from .utilities import Cord_Sort,findlaneCurvature

def LanePoints(midlane,outerlane,offset):
    mid_cnts  = cv2.findContours(midlane,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1]
    outer_cnts  = cv2.findContours(outerlane,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1]

    if mid_cnts and outer_cnts:
        mid_cnts_row_sorted = Cord_Sort(mid_cnts, "rows")
        outer_cnts_row_sorted = Cord_Sort(outer_cnts, "rows")
         
        m_rows = mid_cnts_row_sorted.shape[0]
        o_rows = outer_cnts_row_sorted.shape[0]

        m_rows_btm_pt = mid_cnts_row_sorted[m_rows-1,:]
        o_rows_btm_pt = outer_cnts_row_sorted[o_rows-1,:]
        m_rows_top_pt = mid_cnts_row_sorted[0,:]
        o_rows_top_pt = outer_cnts_row_sorted[0,:]

        traj_btm_pt = ( int((m_rows_btm_pt[0] + o_rows_btm_pt[0])/2) ,int((m_rows_btm_pt[1] + o_rows_btm_pt[1])/2))
        traj_top_pt = ( int((m_rows_top_pt[0] + o_rows_top_pt[0])/2) ,int((m_rows_top_pt[1] + o_rows_top_pt[1])/2))

        return traj_btm_pt,traj_top_pt
    
    else:
        return (0,0),(0,0)

def EstimateNonMidMask(MidEdgeROi):
    Mid_Hull_Mask = np.zeros((MidEdgeROi.shape[0], MidEdgeROi.shape[1],1), dtype=np.uint8)
    contours = cv2.findContours(MidEdgeROi,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[1]
    if contours:
        hull_list = []
        contours = np.concatenate(contours)
        hull =  cv2.convexHull(contours)
        hull_list.append(hull)
        Mid_Hull_Mask = cv2.drawContours(Mid_Hull_Mask,hull_list,0,255,-1)
    Non_Mid_Mask=cv2.bitwise_not(Mid_Hull_Mask)
    return Non_Mid_Mask

def FetchInfoAndDisplay(Mid_lane_edge,Mid_lane,Outer_Lane,frame,Offset_correction):
    Traj_lowP, Traj_upP = LanePoints(Mid_lane,Outer_Lane,Offset_correction)

    PerpDist_LaneCentralStart_CarNose= -1000
    if(Traj_lowP!=(0,0)):
        PerpDist_LaneCentralStart_CarNose = Traj_lowP[0] - int(Mid_lane.shape[1]/2)
    curvature = findlaneCurvature(Traj_lowP[0],Traj_lowP[1],Traj_upP[0],Traj_upP[1])

    Mid_lane_edge = cv2.bitwise_and(Mid_lane_edge,Mid_lane)

    Lanes_combined = cv2.bitwise_or(Outer_Lane,Mid_lane)
    ProjectedLane = np.zeros(Lanes_combined.shape,Lanes_combined.dtype)
    cnts = cv2.findContours(Lanes_combined,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[1]

    if cnts:
        cnts = np.concatenate(cnts)
        cnts = np.array(cnts)
        cv2.fillConvexPoly(ProjectedLane, cnts,255)

    Mİd_less_Mask = EstimateNonMidMask(Mid_lane_edge)
    ProjectedLane = cv2.bitwise_and(Mİd_less_Mask,ProjectedLane)

    Lane_drawn_frame = frame
    Lane_drawn_frame[ProjectedLane==255] = Lane_drawn_frame[ProjectedLane==255] + (0,100,0)
    Lane_drawn_frame[Outer_Lane==255] = Lane_drawn_frame[Outer_Lane==255] + (0,0,100)
    Lane_drawn_frame[Mid_lane==255] = Lane_drawn_frame[Mid_lane==255] + (100,0,0)
    Out_image = Lane_drawn_frame 

    cv2.line(Out_image,(int(Out_image.shape[1]/2),Out_image.shape[0]),(int(Out_image.shape[1]/2),Out_image.shape[0]-int (Out_image.shape[0]/5)),(0,0,255),2)
    cv2.line(Out_image,Traj_lowP,Traj_upP,(255,0,0),2)
    if(Traj_lowP!=(0,0)):
        cv2.line(Out_image,Traj_lowP,(int(Out_image.shape[1]/2),Traj_lowP[1]),(255,255,0),2)

    curvature_str ="Curvature = " + f"{curvature:.2f}"
    PerpDist_ImgCen_CarNose_str="Distance = " + str(PerpDist_LaneCentralStart_CarNose)
    textSize_ratio = 0.5
    cv2.putText(Out_image,curvature_str,(10, 30),cv2.FONT_HERSHEY_DUPLEX,textSize_ratio,(0,255,255),cv2.LINE_AA )
    cv2.putText(Out_image,PerpDist_ImgCen_CarNose_str,(10, 50),cv2.FONT_HERSHEY_DUPLEX,textSize_ratio,(0,255,255),1,cv2.LINE_AA )
   
    return PerpDist_ImgCen_CarNose_str,curvature

also midlane_estimation code

import cv2
import numpy as np
import math

def Distance_(a,b):
    return math.sqrt( ( (a[1]-b[1])**2 ) + ( (a[0]-b[0])**2 ) )

def ApproxDistBWCntrs(cnt,cnt_cmp):
    # compute the center of the contour
    M = cv2.moments(cnt)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    # compute the center of the contour
    M_cmp = cv2.moments(cnt_cmp)
    cX_cmp = int(M_cmp["m10"] / M_cmp["m00"])
    cY_cmp = int(M_cmp["m01"] / M_cmp["m00"])
    minDist=Distance_((cX,cY),(cX_cmp,cY_cmp))
    Centroid_a=(cX,cY)
    Centroid_b=(cX_cmp,cY_cmp)
    return minDist,Centroid_a,Centroid_b

def RetLargestContour(gray):
    LargestContour_Found = False
    thresh=np.zeros(gray.shape,dtype=gray.dtype)
    _,bin_img = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
    #Find the two Contours for which you want to find the min distance between them.
    cnts = cv2.findContours(bin_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]
    Max_Cntr_area = 0
    Max_Cntr_idx= -1
    for index, cnt in enumerate(cnts):
        area = cv2.contourArea(cnt)
        if area > Max_Cntr_area:
            Max_Cntr_area = area
            Max_Cntr_idx = index
            LargestContour_Found = True
    if (Max_Cntr_idx!=-1):
        thresh = cv2.drawContours(thresh, cnts, Max_Cntr_idx, (255,255,255), -1) # [ contour = less then minarea contour, contourIDx, Colour , Thickness ]
    return thresh, LargestContour_Found



def estimate_midlane(midlane_patches, Max_dist):
#1 
    midlane_connectivity_bgr = cv2.cvtColor(midlane_patches,cv2.COLOR_GRAY2BGR)
#2   
    cnts= cv2.findContours(midlane_patches,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[1]
#3
    min_area = 1
    legit_cnts = []
    for _,cnt in enumerate(cnts):
       cnt_area = cv2.contourArea(cnt)
       if (cnt_area>min_area):
           legit_cnts.append(cnt)
    cnts = legit_cnts
  

    CntIdx_BstMatch = []# [BstMatchwithCnt0,BstMatchwithCnt1,....]
    for index, cnt in enumerate(cnts):
        prevmin_dist = 100000 ; Bstindex_cmp = 0 ; BstCentroid_a=0  ; BstCentroid_b=0      
        for index_cmp in range(len(cnts)-index):
            index_cmp = index_cmp + index
            cnt_cmp = cnts[index_cmp]

            if (index!=index_cmp):
                min_dist,cent_a,cent_b  = ApproxDistBWCntrs(cnt,cnt_cmp)             
                if(min_dist < prevmin_dist):
                    if(len(CntIdx_BstMatch)==0):
                        prevmin_dist = min_dist
                        Bstindex_cmp = index_cmp
                        BstCentroid_a = cent_a
                        BstCentroid_b = cent_b
                    else:
                        already_present= False
                        for i in range(len(CntIdx_BstMatch)):
                            if( (index_cmp == i)and (index == CntIdx_BstMatch[i])):
                                already_present= True
                            if not already_present:
                                prevmin_dist = min_dist
                                Bstindex_cmp = index_cmp
                                BstCentroid_a = cent_a
                                BstCentroid_b = cent_b


        if ((prevmin_dist!=100_000 ) and (prevmin_dist>Max_dist)):
            break
        if (type(BstCentroid_a)!=int):
            CntIdx_BstMatch.append(Bstindex_cmp)
            cv2.line(midlane_connectivity_bgr,BstCentroid_a,BstCentroid_b,(0,255,0),2)

    midlane_connectivity= cv2.cvtColor(midlane_connectivity_bgr,cv2.COLOR_BGR2GRAY)

 #largest contour

    estimated_midlane, largest_found = RetLargestContour(midlane_connectivity)

    if largest_found:
        return estimated_midlane
    else:
        return midlane_patches

New contributor

mina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật