ROI目标区域截取(Python/OpenCV)

问题:当进行目标检测,获取输出检测目标print( top, left, bottom, right)的左上顶点和右下顶点两个点坐标,怎么将目标区域截取下来?
首先提供我使用的原图:
ROI目标区域截取(Python/OpenCV)_第1张图片

下面针对于下方原图黄色方框中的车辆目标区域截取举例:
ROI目标区域截取(Python/OpenCV)_第2张图片

print(top, left, bottom, right)=(570,652,762,949)
方法:
ROI目标区域截取(Python/OpenCV)_第3张图片

pyhton代码:
方法一:PIL模块
1、img_car1

from PIL import Image

#img=cv2.imread("G:\Deep Code\yolov4-pytorch-master\img\street.jpg")
img=Image.open("G:\Deep Code\yolov4-pytorch-master\img\street.jpg")


#img1=cv2.imread("G:\Deep Code\yolov4-pytorch-master\img_out\street.jpg")
#print( top, left, bottom, right)
#      -------------------y
#      | 
#      |    .(left,top)
#      |    |    |    
#      |    |    |
#      |    |    .(right,bottom)
#      x
img_car1=img.crop(( 652,570 ,949,762))
img_car2=img.crop(( 0,598 , 52,687))
img_car_suv=img.crop((577,545  ,770,718))
img_wheel1=img.crop(( 664,689 , 692,757))
img_wheel2=img.crop(( 748,692 ,780,766))
#img3=img.copy(692, 748, 766 ,780) 错误语法
# cv2.imshow("2",img1)
# cv2.imshow("1",img)
img_car1.save("./img_car1.jpg")
img_car2.save("./img_car2.jpg")
img_car_suv.save("./img_car_suv.jpg")
img_wheel1.save("./img_wheel1.jpg")
img_wheel2.save("./img_wheel2.jpg")
#cv2.waitKey(0)

2、img5_car1

from PIL import Image
img=Image.open("G:\Deep Code\yolov4-pytorch-master\img\street.jpg")

y=570
x=652
h=192
w=297
img5_car1=img.crop((x,y,x+w,y+h))
img5_car1.save("./img5_car1.jpg")

方法二:cv2模块

img3_car1


import cv2 as cv
import os

#print( top, left, bottom, right) : print(car1)=(( 692,748 ,766,780))
#      ------------------->y
#      | 
#      |    .(left,top)
#      |    |    |    
#      |    |    |
#      |    |    .(right,bottom)
#      x
img=cv.imread("G:\Deep Code\yolov4-pytorch-master\img\street.jpg")
#img1_car1=img[( 748,692 ,780,766)]
img1_car1=img[int( 748-32):int(748+32),int(692-74):int(692+74)]
cv.imwrite("img1_car1.jpg",img1_car1)
img2_car1=img[int( 780-32):int(780+32),int(766-74):int(766+74)]
cv.imwrite("img2_car1.jpg",img2_car1)

img3_car1=img[int(666-96):int(666+96),int(810-158):int(810+158)]
img4_car1=img[int(800-158):int(800+158),int(666-96):int(666+96)]
cv.imwrite("img3_car1.jpg",img3_car1)
cv.imwrite("img4_car1.jpg",img4_car1)

#img1_car2=img[( 0,598 , 52,687)]

#img1_car_suv=img[(577,545  ,770,718)]
#img1_wheel1=img[( 664,689 , 692,757)]
#img1_wheel2=img[( 652,570 ,949,762)]
#cv.imrite("img1_car1",img1_car1)
#cv.imrite("img1_car2",img1_car2)
#cv.imrite("img1_car_suv",img1_car_suv)
#cv.imrite("img1_wheel1",img1_wheel1)
#cv.imrite("img1_wheel2",img1_wheel2)

cv.waitKey(0)

OpenCV4(c++)代码:
img6_car1/img7_car1

#include
#include
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("G:/Deep Code/street.jpg");
	if (img.empty()) {
		cout << "the picture not true" << endl;
		return -1;
	}
	//截图方法一
	Mat img6_car1;
	Mat img7_car1;
	Rect rect( 652,570, 297,192);
	img6_car1 = img(rect);

	// 截图方法二(570, 762)
	img7_car1 = img( Range(570, 762), Range(652, 949));
	namedWindow("1", WINDOW_NORMAL);
	imshow("1", img);
	imshow("img6_car1", img6_car1);
	imshow("img7_car1", img7_car1);
	imwrite("img6_car1.jpg", img6_car1);
	imwrite("img7_car1.jpg", img7_car1);
	waitKey(0);

	return 0;
}

以上5个区域截取都成功的将该ROI获取下来:
ROI目标区域截取(Python/OpenCV)_第4张图片

你可能感兴趣的:(Python,opencv,python,计算机视觉)