import numpy as np
import cv2
import copy
from PIL import Image
# mask
# def mask_oprition(mask_path,img_path):
# #resize
# size = 640
# img = cv2.imread(img_path)
# height, width = img.shape[:2] # 得到行和列的长度
# scale=size/height # 得到640和图像高的缩放比例
# height=size
# width=int(width*scale)
# img = cv2.resize(img, (width, height))
# 读者自行添加mask
# mask
# mask = cv2.imread(mask_path)
# rows,cols,channels = img.shape
# gray_back = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
# for i in range(rows):
# for j in range(cols):
# if gray_back[i,j]==0:#0代表黑色的点
# img[i,j]=255#此处替换颜色,为BGR通道
# cv2.imwrite('test_dataset/imgs/masked.jpg',img)
#crop
def crop_single_picture(img_path):
#step1:加载图片,转成灰度图,并用Sobel算子计算x,y方向上的梯度,之后在x方向上减去y方向上的梯度,通过这个减法,我们留下具有高水平梯度和低垂直梯度的图像区域。
image = cv2.imread(img_path)
shape = image.shape
image_row = shape[0]
image_col = shape[1]
#显示图片长宽
# print(image_row,image_col)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 得到灰度图
gradX = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1) # x方向一阶导数
gradY = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1) # y方向一阶导数
# 从x梯度中减去y梯度
gradient = cv2.subtract(gradX, gradY)
#显示梯度图
cv2.imshow("gradient1", gradient)
gradient = cv2.convertScaleAbs(gradient) # 将图像转回uint8形式
cv2.imshow("gradient2", gradient)
#step2:去除图像上的噪声。
blurred = cv2.blur(gradient, (9, 9)) # 滤波,去除噪声
(_, thresh) = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY) # 图像二值化,thresh为得到的图像
thresh = cv2.erode(thresh, None, iterations=5)
thresh = cv2.dilate(thresh, None, iterations=4)
#step3:找出图像所有的轮廓
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # cnts为得到的轮廓,存放在一个list中
print('轮廓框的个数:{}'.format(len(cnts)))
all_box_img = copy.deepcopy(image) # 深复制,跟原图没有关系,一个改变不影响另一个
cv2.drawContours(all_box_img, cnts, -1, (0, 255, 0), 1) # 在all_box_img上画轮廓
count = 0
boxs =[]
for i, contour in enumerate(cnts):
area = cv2.contourArea(contour) # 计算包围形状的面积
if area < 550: # 读者自行更改:此时包围形状面积小于550的过滤
continue
count += 1
rect = cv2.minAreaRect(contour) # 检测轮廓最小外接矩形,得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
box = np.int0(cv2.boxPoints(rect))
boxs.append(box) # 最后剩下的有用的框
final_box_img = copy.deepcopy(image)
print('过滤后轮廓框的个数:{}'.format(len(boxs)))
cv2.drawContours(final_box_img, boxs, -1, (0, 255, 0), 1) # 把剩余的框画到原图上
# 裁剪
for i,box in enumerate(boxs):
xs = [j[0] for j in boxs[i]]
ys = [j[1] for j in boxs[i]]
x1 = min(xs)
x2 = max(xs)
y1 = min(ys)
y2 = max(ys)
if x1 < 0:
x1 = 0
if x2 > image_col:
x2 = image_col
if y1 < 0:
y1 = 0
if y2 > image_row:
y2 = image_row
print(x1,x2,y1,y2)
hight = y2 - y1
width = x2 - x1
cropImg = image[y1:y1+hight, x1:x1+width]
cv2.imshow(f"croped{i}", cropImg)
cv2.imwrite(f"shiyings/{i}.jpg", cropImg)
# 读者自行按照需要显示相应的状态图片
# cv2.imshow("blurred", blurred)
# cv2.imshow("gradient", gradient)
# cv2.imshow("thresh", thresh)
# cv2.imshow("all_box_img", all_box_img)
# cv2.imshow("final_box_image", final_box_img)
cv2.waitKey(0)
if __name__ == '__main__':
# 图片路径
img_path = ''
# mask路径
#mask_path = 'test_dataset/imgs/in_white_resized.jpg'
# 保存路径
path = ''
#mask_oprition(mask_path,img_path)
crop_single_picture(path)
python利用图像处理方法 实现多目标定位与裁剪(源代码).zip
版权申诉
16 浏览量
2023-09-25
14:23:35
上传
评论
收藏 2KB ZIP 举报
处处清欢
- 粉丝: 158
- 资源: 2511
最新资源
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈