赞
踩
目录
- import cv2
-
- # 读取图片
- image = cv2.imread('image.jpg')
-
- # 显示图片
- cv2.imshow('Image', image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
-
- # 读取图片
- image = cv2.imread('image.jpg')
-
- # 调整大小
- resized_image = cv2.resize(image, (new_width, new_height))
-
- # 裁剪
- cropped_image = image[y1:y2, x1:x2]
-
- # 旋转
- rows, cols = image.shape[:2]
- M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, scale)
- rotated_image = cv2.warpAffine(image, M, (cols, rows))
-
- # 滤波
- blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigmaX)
-
- # 保存图片
- cv2.imwrite('processed_image.jpg', processed_image)
cv2.putText # 文本标注
cv2.rectangle # 矩形标注
cv2.circle # 圆形标注
cv2.arrowedLine # 箭头标注
cv2.polylines # 多边形标注
cv2.ellipse # 椭圆标注
cv2.drawContours # 轮廓
cv2.fillPoly # 多边形填充
- # -*- coding: utf-8 -*-
- # @Author: GraceJiang
- # @Date: 2024/4/24
- # @Description:
-
- import cv2
-
- # 读取图片
- image = cv2.imread(r'F:\pythonScript\pythonProject\AI\annotation_Learning\testA\1.jpg')
-
- # 添加文本标注
- text = "Example Annotation"
- org = (50, 50) # 标注的起始位置
- font = cv2.FONT_HERSHEY_SIMPLEX
- font_scale = 1
- color = (255, 0, 0) # 标注的颜色,这里是蓝色
- thickness = 2
- image_with_annotation = cv2.putText(image, text, org, font, font_scale, color, thickness)
-
- # 添加矩形标注
- start_point = (100, 100) # 矩形的左上角坐标
- end_point = (200, 200) # 矩形的右下角坐标
- color = (0, 255, 0) # 标注的颜色,这里是绿色
- thickness = 2
- image_with_annotation = cv2.rectangle(image_with_annotation, start_point, end_point, color, thickness)
-
- # 显示带有标注的图片
- cv2.imshow('Image with Annotation', image_with_annotation)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
-
- # 读取图片
- image = cv2.imread(r'F:\pythonScript\pythonProject\AI\annotation_Learning\testA\1.jpg')
-
- # 在图片上画圆
- center_coordinates = (300, 300) # 圆心的坐标
- radius = 100 # 圆的半径
- color = (0, 255, 0) # BGR格式的颜色值,这里是绿色
- thickness = 2 # 线条的厚度
- image_with_circle = cv2.circle(image, center_coordinates, radius, color, thickness)
-
- # 显示带有圆形标注的图片
- cv2.imshow('Image with Circle', image_with_circle)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
-
- # 读取图片
- image = cv2.imread(r'F:\pythonScript\pythonProject\AI\annotation_Learning\testA\1.jpg')
-
- # 定义箭头起点和终点坐标
- start_point = (100, 100) # 箭头起点的坐标
- end_point = (300, 300) # 箭头终点的坐标
- color = (255, 0, 255) # BGR格式的颜色值,这里是紫色
- thickness = 2 # 线条的厚度
- image_with_arrow = cv2.arrowedLine(image, start_point, end_point, color, thickness)
-
- # 显示带有箭头标注的图片
- cv2.imshow('Image with Arrow', image_with_arrow)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
-
- # 读取图片
- image = cv2.imread(r'F:\pythonScript\pythonProject\AI\annotation_Learning\testA\1.jpg')
-
- # 定义多边形顶点坐标
- pts1 = np.array([[100, 50], [200, 300], [700, 200], [500, 100]], np.int32)
- pts1 = pts1.reshape((-1, 1, 2))
- color1 = (0, 255, 0) # 绿色
-
- pts2 = np.array([[10, 50], [400, 50], [90, 200], [50, 500]], np.int32)
- pts2=pts2.reshape((-1, 1, 2))
- color2 = (255, 0, 0) # 蓝色
-
- # 在图片上绘制多边形
- color = (0, 0, 255) # BGR格式的颜色值,这里是红色
- thickness = 2 # 线条的厚度
- image_with_polygon = cv2.polylines(image, [pts1], isClosed=True, color=color1, thickness=thickness)
- image_with_polygon = cv2.polylines(image_with_polygon, [pts2], isClosed=True, color=color2, thickness=thickness)
-
- # 显示带有多边形标注的图片
- cv2.imshow('Image with Polygon', image_with_polygon)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
-
- # 创建一个空白图像
- img = np.zeros((512, 512, 3), np.uint8)
-
- # 定义椭圆参数
- center = (256, 256) # 椭圆中心坐标
- axes = (150, 100) # 长轴长度和短轴长度
- angle = 45 #旋转角度
- startAngle = 0 # 起始角度
- endAngle = 360 # 结束角度
- color = (0, 255, 0) # 颜色,这里使用绿色
-
- # 绘制椭圆
- image_with_ellipse = cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness=3)
-
- # 显示绘制的图像
- cv2.imshow('Ellipse', image_with_ellipse)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
-
- # 创建一个黑色的图像
- img = np.zeros((512,512,3), np.uint8)
-
- # 定义轮廓坐标
- contours = np.array([[100, 100], [300, 100], [200, 300]])
-
- # 将轮廓转换为OpenCV需要的格式
- contours = contours.reshape((-1, 1, 2))
-
- # 在图像上绘制轮廓
- cv2.drawContours(img, [contours], -1, (0,255,0), 3)
-
- # 显示绘制的图像
- cv2.imshow('Contours', img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
-
- # 创建一个黑色的图像
- img = np.zeros((512,512,3), np.uint8)
-
- # 定义多边形的顶点坐标
- pts = np.array([[10, 50], [400, 50], [90, 200], [50, 500]], np.int32)
- pts = pts.reshape((-1,1,2))
-
- # 在图像上绘制并填充多边形
- cv2.fillPoly(img, [pts], color=(0,255,0))
-
- # 显示绘制的图像
- cv2.imshow('Filled Polygon', img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
-
- # 生成一个示例图像
- img = np.zeros((300, 300, 3), np.uint8)
- cv2.rectangle(img, (50, 50), (200, 200), (255, 255, 255), -1) # 画一个白色矩形
-
- # 寻找轮廓
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- ret, thresh = cv2.threshold(gray, 127, 255, 0)
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-
- # 复制原始图像用于绘制外接矩形和闭圆
- img_with_rect = img.copy()
- img_with_circle = img.copy()
-
- for contour in contours:
- # 绘制轮廓外接矩形
- x, y, w, h = cv2.boundingRect(contour)
- cv2.rectangle(img_with_rect, (x, y), (x+w, y+h), (0, 255, 0), 2)
-
- # 绘制最小闭圆
- (cx, cy), radius = cv2.minEnclosingCircle(contour)
- center = (int(cx), int(cy))
- radius = int(radius)
- cv2.circle(img_with_circle, center, radius, (0, 0, 255), 2)
-
- # 显示处理后的图像
- cv2.imshow('Bounding Rectangle', img_with_rect)
- cv2.imshow('Min Enclosing Circle', img_with_circle)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
将图像转换为灰度图像,然后进行阈值化处理,最后找到图像中的轮廓存储在contours中.
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
: 将彩色图像转换为灰度图像。在处理图像时,通常将其转换为灰度图像有助于简化处理,并减少计算量。
ret, thresh = cv2.threshold(gray, 127, 255, 0)
: 对灰度图像进行阈值化处理。阈值化是将图像转换为二值图像的过程,即将像素值分为两类:大于阈值的部分设为255(白色),小于阈值的部分设为0(黑色)。阈值为127,超过该阈值的像素值被设为255。
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
: 找到图像中的轮廓。
cv2.findContours()
函数用于在二值图像中查找轮廓,返回所有轮廓的列表。参数thresh
是二值图像,cv2.RETR_EXTERNAL
表示只检测外部轮廓,cv2.CHAIN_APPROX_SIMPLE
表示只保留轮廓的端点信息,以节省内存。_
通常被用作一个占位符,表示接收的值将被丢弃,不会在后续的代码中使用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。