opencvcv.line

'''本次来学习基于opencv进行各种画图操作 , 以前只习惯用matplotlib,最近开始用opencv,觉得也很好用 。cv.line(), cv.circle() , cv.rectangle(), cv.ellipse(), cv.putText() In all the above functions, you will see some common arguments as given below:img : The image where you want to draw the shapescolor : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.thickness : Thickness of the line or circle etc. If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv.LINE_AA gives anti-aliased line which looks great for curves.'''####################################### cv2.line 官方的解释如下'''void cv::line ( InputOutputArrayimg,Pointpt1,Pointpt2,const Scalar &color,intthickness = 1,intlineType = LINE_8,intshift = 0)Python:cv.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]] ) -> img各个参数的含义如下:img Image.pt1 First point of the line segment.pt2 Second point of the line segment.color Line color.BGR顺序thickness Line thickness.lineType Type of the line. See LineTypes.shift Number of fractional bits in the point coordinates.'''import cv2 as cv# Create a black imageimg = cv.imread('messi.jpg')# Draw a diagonal blue line with thickness of 5 pxcv.line(img,(0,0),(511,511),(255,0,0),lineType = cv.LINE_AA,shift =0)cv.imshow('img line ',img)if cv.waitKey(0) & 0xff==ord('q'):cv.destroyAllWindows()###################rectangle 官方解读如下'''void cv::rectangle ( InputOutputArrayimg,Pointpt1,Pointpt2,const Scalar &color,intthickness = 1,intlineType = LINE_8,intshift = 0)Python:cv.rectangle( img, pt1, pt2, color[, thickness[, lineType[, shift]]] ) -> imgcv.rectangle( img, rec, color[, thickness[, lineType[, shift]]] ) -> imgimg Image.pt1 Vertex of the rectangle.pt2 Vertex of the rectangle opposite to pt1 .color Rectangle color or brightness (grayscale image).thickness Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle.lineType Type of the line. See LineTypesshift Number of fractional bits in the point coordinates.我喜欢首先通过C++版本的函数来看函数 , 因为输入输出数据类型一目了然 。从参数来看,输入第一个参数是图像,第2,3个参数是坐标点,严格来说是矩形左上角、右下角的两点 , 紧接着是颜色,这里也是,如果是灰度可以只有一个scalar,如果是彩色,按照BGR顺序传参 。接着的参数thickness是线的厚度,肯定是这个数越大 , 画出来的线越宽 。然后是线的类型,可以看一下一下几种可选,其中LINE_8是默认的 。这里线的类型和我们普通意义上的是实线、虚线等这种不一样 , 由于我们这里画线其实是改变了image像素点的颜色,因此这里的线的类型是这个意义 , 就是都是怎么改变像素点颜色的算法,具体可以参考官方文档 。LINE_4 Python: cv.LINE_4 4-connected lineLINE_8 Python: cv.LINE_8 8-connected lineLINE_AA Python: cv.LINE_AA antialiased line'''import cv2img = cv2.imread(r'messi.jpg')print(img.shape)cv2.rectangle(img, (120, 10), (240, 100), (0, 255, 0), 2)cv2.imshow("rectangle", img)if cv.waitKey(0) & 0xff==ord('q'):cv.destroyAllWindows()'''circlevoid cv::circle ( InputOutputArrayimg,Pointcenter,# 圆心intradius,# 半径const Scalar &color,intthickness = 1,intlineType = LINE_8,intshift = 0)Python:cv.circle( img, center, radius, color[, thickness[, lineType[, shift]]] ) -> img'''cv2.circle(img,(80,80),30,(0,0,255),-1)cv2.imshow('img',img)if cv.waitKey(0) & 0xff==ord('q'):cv.destroyAllWindows()'''textvoid cv::putText ( InputOutputArrayimg,const String &text,Pointorg,intfontFace,doublefontScale,Scalarcolor,intthickness = 1,intlineType = LINE_8,boolbottomLeftOrigin = false)Python:cv.putText( img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] ) -> img'''import timeimport cv2cap = cv2.VideoCapture(0)while (1):ret, img = cap.read()text = 'camera is ready.'import randomindx = random.randint(0,len(text))# 图片 添加的文字 位置 字体 字体 大小 字体颜色 字体粗细cv2.putText(img, text, (5,5+indx*10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)cv2.imshow("image", img)if cv2.waitKey(0) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()#Tips:此处注意,是两个点,但是很多数据集给的标注信息是左上角的点和宽高,所以你要转换一次 。同时各个地方坐标点是整数值,这个可千万要记住,#否则提示很多错误 。其实也很好理解,这几种方法是通过改变图像像素点像素值来进行画图的,因此必须是整数 。
【opencvcv.line】

    推荐阅读