python - 如何从屏幕中提取选定的矩形作为顶视图图像?
问题描述
我编写了一些代码来检测图像中的计算机屏幕。我需要对位于该选定矩形中心的像素做一些工作。如何将选定的矩形提取为矩形图像?
import imutils
import cv2
image = cv2.imread('test-img/imgRec3.jpg')
ratio = image.shape[0] / 300.0
image = imutils.resize(image, height=300)
realImage = image.copy()
# convert the image to grayscale, blur it, and find edges in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
# loop over our contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.015 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("image", realImage)
cv2.imshow("Screen Rec", image)
cv2.waitKey(0)
提前感谢您的帮助。
解决方案
使用 skimage 你可以这样做:
def transform(intersections, image):
w,h = get_orientation()
a = np.array([0,h])
b = np.array([w,h])
c = np.array([w,0])
d = np.array([0,0])
tf = skimage.transform.estimate_transform("projective",
dst=np.vstack((a,b,c,d)),
src=intersections)
invtf = tf.inverse
transformedImage = skimage.transform.warp(image=image,inverse_map=invtf, output_shape=(h,w))
return transformedImage
fig,(ax0,ax1) = plt.subplots(ncols=2, figsize=(15,8))
transformed_image = transform(sorted_intersec, img[index] )
ax0.imshow(transformed_image,cmap="gray")
ax1.imshow(img[index])
交叉点是你的 4 条边。请记住,您必须以正确的顺序放置它们。
推荐阅读
- python - Python 请求意外关闭,没有得到响应
- ios - Swift - CABasicAnimation 完成时更新 UIButton
- javascript - 逐图隐藏和显示幻灯片 - Javascript
- ibm-cloud - 如何为 Vyatta 获取“SoftLayer_Product_PackageInitParameters”
- mysql - AWS RDS 角色类型:主与实例
- jenkins - 在阶段内添加带有参数的构建?
- c++ - 在 C++ 中输入时未检测到换行符
- c# - 我如何找出计划属于哪个选项卡/位置?
- javascript - 如何更新 redis 中的多键哈希项?
- angular - AngularFire2 orderByChild 查询不成功