Home > other >  Always appear AttributeError: 'NoneType' object has no attribute 'copy' why is t
Always appear AttributeError: 'NoneType' object has no attribute 'copy' why is t

Time:10-31

 import cv2 as CV 
The import numpy as np
The import math


Def findColor (SRC, min, Max) :
"'
Description: used to determine the color recognition based on HSV
: param SRC:
: param min:
: param Max:
: return:
"'
Src_image=CV. GaussianBlur (SRC, (9, 9), 0)
Hsv_image=CV. CvtColor (src_image, CV. COLOR_BGR2HSV)
Thresholded=CV. InRange (hsv_image (min, 90, 90), (Max. 255, 255))
Return thresholded


Def Angle (pt1, pt2 pt0) :
"'
Description: used to prove the square
: param pt1:
: param pt2:
: param pt0:
: return:
"'
Dx1=pt1 [0] [0] - pt0 [0] [0]
Dy1=pt1 [0] [1] - pt0 [0] [1]
Dx2=pt2 [0] [0] - pt0 [0] [0]
Dy2=pt2 [0] [1] - pt0 [0] [1]
# side length of square than
Thewire=(dx1 + dy1 dx1 * * dy1)/(dx2 + dy2 dx2 * * dy2)
A=(dx2 + dy1 dx1 * * dy2)/math.h SQRT ((dy1) dx1 + dy1 dx1 * * * (dx2 + dy2 dx2 * * dy2) + 1 e - 10)
# according to the square of length ratio is too small or too big advance eliminate the quadrangle, if out too much, adjusting the proportion of digital
If thewire & lt; 0.8 or 1.2 & lt; Thewire:
# according to the square of length ratio is too small or too large advance eliminate the quadrilateral
The return of 1.0
Return a


Def Graphicdetection (srcImage, CLImage outImage, STR) :
"'
Description: a number used to identify and statistical shape
: param srcImage:
: param CLImage:
: param outImage:
: param STR:
: return:
"'
BjImage=srcImage. Copy ()
# fuzzy noise reduction
CV. GaussianBlur (CLImage, (9, 9), and 0, CLImage, 0)
# edge detection
OutImage=CV. Canny (CLImage, 10, 100)
# define a square counter
Zfcount=0
# define a triangle counter
Sjcount=0
# locate profile
CloneImage, contours, hierarchy=CV. FindContours (outImage, CV. RETR_EXTERNAL, CV. CHAIN_APPROX_NONE)
For I in range (len (contours) :
# calculating contour moment
Mu=CV. Moments (contours [I], False)
# compute contour centroid
Cx=mu [' m10 ']/mu [' m00]
Cy=mu [' m01 ']/mu [' m00]
Draw the center #
CV. Circle (bjImage, (np) int (cx), np. Int (cy), 5 (0, 0, 0) and 1, 8, 0)
# draw polygons
Approx=CV. ApproxPolyDP (contours [I], CV. ArcLength (contours [I], True) * 0.02, True)
If len (approx)==4 & amp; Np. Int (math.h fabs (CV. ContourArea (approx))) & gt; 1000 & amp; CV. IsContourConvex (approx) :
MaxCosine=0
For j in range (2, 5) :
# square
Cosine=math.h fabs (Angle (approx [j % 4], approx [j - 2], approx] [j - 1))
MaxCosine=np. Maximum (MaxCosine, cosine)
If MaxCosine & lt; 0.3:
Jx=approx
Mapped the square frame #
CV. The line (bjImage, (jx [0] [0] [0], jx [0] [0] [1]), (jx [1] [0] [0], jx [1] [0] [1]), (0, 0, 0), (3)
CV. The line (bjImage, (jx [1] [0] [0], jx [1] [0] [1]), (jx [2] [0] [0], jx [2] [0] [1]), (0, 0, 0), (3)
CV. The line (bjImage, (jx [2] [0] [0], jx [2] [0] [1]), (jx [3] [0] [0], jx [3] [0] [1]), (0, 0, 0), (3)
CV. The line (bjImage, (jx [3] [0] [0], jx [3] [0] [1]), (jx [0] [0] [0], jx [0] [0] [1]), (0, 0, 0), (3)
Tam="Square" + STR
CV. PutText (bjImage, tam, (np) int (cx), np. Int (cy) - 10), CV. FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
Zfcount +=1
# trilateral form
If len (approx)==3:
SJX=approx
Mapped the triangle frame #
CV. The line (bjImage, (SJX [0] [0] [0], SJX [0] [0] [1]), (SJX [1] [0] [0], SJX [1] [0] [1]), (0, 0, 0), (3)
CV. The line (bjImage, (SJX [1] [0] [0], SJX [1] [0] [1]), (SJX [2] [0] [0], SJX [2] [0] [1]), (0, 0, 0), (3)
CV. The line (bjImage, (SJX [2] [0] [0], SJX [2] [0] [1]), (SJX [0] [0] [0], SJX [0] [0] [1]), (0, 0, 0), (3)
Tam="Triangle" + STR
CV. PutText (bjImage, tam, (np) int (cx), np. Int (cy) - 10), CV. FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
Sjcount +=1
# hoff detection round
Circles=None
# hoff circle detection
Circles=CV. HoughCircles (CLImage, CV. HOUGH_GRADIENT, 1.5 to 10, circles, 200, 100, 0, 0)
Circles=np. Uint16 (np) around (circles))
For I circles in [0, :] :
The radius=(I [2]);
Tam="Round" + STR
CV. PutText (bjImage, tam, (I [0], [1] - I 10), CV. FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
CV. Circle (bjImage, (I [0], I [1]), 3, (0, 0, 0) and 1, 8, 0)
# draw round contour
CV. Circle (bjImage, (I [0], I [1]), the radius, (0, 0, 0), 3, 8, 0)

If the STR=="R" :
STR="red"
A square has print (" % s % d "% (STR, zfcount))
Triangle has print (" % s % d "% (STR, sjcount))
Circle there are % d print (" % s "% (STR, len (circles)))
If the STR=="G" :
STR="green"
A square has print (" % s % d "% (STR, zfcount))
Triangle has print (" % s % d "% (STR, sjcount))
Circle there are % d print (" % s "% (STR, len (circles)))
If the STR=="Y" :
STR="yellow"
A square has print (" % s % d "% (STR, zfcount))
Triangle has print (" % s % d "% (STR, sjcount))
Circle there are % d print (" % s "% (STR, len (circles)))

Return bjImage


SRC=https://bbs.csdn.net/topics/cv.imread (" 1. JPG ")
DST=SRC. Copy ()

Rdst=findColor (SRC, 0, 25)
Gdst=findColor (SRC, 60, 80)
Ydst=findColor (SRC, 30, 50) [code=python]


RdstImage=Graphicdetection (SRC, Rdst, DST, "R")
Gdst GdstImage=Graphicdetection (SRC, DST, "G")
YdstImage=Graphicdetection (SRC, Ydst, DST, "Y")

CV. Imshow (Rdst, RdstImage)
CV. Imshow (" Gdst GdstImage)
CV. Imshow (" Ydst YdstImage)

CV. WaitKey (0)
CV. DestroyAllWindows () [/code]
  • Related