# Copy the image
img_rgb_copy = img_rgb.copy()
# Draw a rectangle
cv2.rectangle(img_rgb_copy, pt1 = (405, 90), pt2 =(740, 510),
color = (255, 0, 0), thickness = 5)
plt.axis("off")
plt.imshow(img_rgb_copy)
pt1 是左上角坐标,pt2 是右下角坐标,坐标可以用电脑自带的画图工具获取,直接 copy 过来就行!
补充1:如果要画成 mask 块,也即全填充的形式,则把 thickness
参数设置为 -1 即可
补充2:如果要把区域裁剪出来,可以借助 numpy 的切片操作,例如
import cv2
img_pth = "/file2/yanmeng/human/ocean3_115/images/ocean_17_55034.jpg"
img = cv2.imread(img_pth)
box = (606, 182, 682, 360) # (x0, y0, x1, y1)
crop_img = img[box[1]:box[3], box[0]:box[2]] # [y0:y1, x0:x1]
cv2.imshow("1", img)
cv2.imshow("2", crop_img)
cv2.waitKey(10000)
cv2.destroyAllWindows() # 释放所有由 Opencv 创建的窗口
waitKey()
的参数为等待键盘触发的时间,单位为 ms,返回值为 -1(表示没有键被按下)或者 ASCII 码
复制 RoI 区域
import cv2
img = cv2.imread("1.jpg")
bag = img[840:1004, 830:1032]
img[840:1004, 830+200:1032+200] = bag
cv2.imwrite("2.jpg", img)
void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color,
int thickness=1, int line_type=8, int shift=0 );
img_rgb_copy = img_rgb.copy()
# Draw a circle
cv2.circle(img_rgb_copy, center = (200, 280), radius =80,
color = (0,0,255), thickness = 5)
plt.axis("off")
plt.imshow(img_rgb_copy)
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = np.zeros((512,512,3),np.uint8)#生成一个空彩色图像
cv2.ellipse(img=img,
center=(256,256),
axes=(150,100),
angle=30,
startAngle=45,
endAngle=180,
color=(0,0,255),
thickness=-1)
#注意最后一个参数-1,表示对图像进行填充,默认是不填充的,如果去掉,只有椭圆轮廓了
cv2.imshow("1", img)
cv2.waitKey(0)
RCNN 和 Fast RCNN 都用的是 SS,目前被主流的 Faster RCNN 的 Region Proposal Network 替代!我们重温下经典!
我用的 jupyter notebook,所以要克服下 opencv 的 cv2.imshow()
问题,参考
opencv如何在jupyter notebook中显示图片
import cv2
import matplotlib.pyplot as plt
if __name__ == '__main__':
cv2.setUseOptimized(True);
cv2.setNumThreads(4);
# read image
im = cv2.imread('/root/userfolder/Experiment/1.png')
# resize image
newHeight = 200
newWidth = int(im.shape[1] * 200 / im.shape[0])
im = cv2.resize(im, (newWidth, newHeight))
#cv2.imshow("input", im)
# jupyter notebook
#img = im[:,:,::-1] # 必须为 ::-1
#plt.imshow(im)
# 创建算法+设置输入图像
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(im)
# 使用SS快速版本
ss.switchToSelectiveSearchFast()
# 执行SS
rects = ss.process()
print('Total Number of Region Proposals: {}'.format(len(rects)))
# 推荐100个ROI
numShowRects = 100
imOut = im.copy()
# 显示前100个区域外接矩形框
for i, rect in enumerate(rects):
if i < numShowRects:
x, y, w, h = rect
cv2.rectangle(imOut, (x, y), (x + w, y + h), (0, 255, 0), 1, cv2.LINE_AA)
else:
break
# show output
"""
cv2.imshow("SS-Demo", imOut)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
# jupyter notebook
img = imOut[:,:,::-1] # 必须为 ::-1
plt.xticks(())
plt.yticks(())
plt.imshow(img)
cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation)