简体   繁体   English

如何获取图像内矩形内的像素值

[英]How to get pixel values inside of a rectangle within an image

I have an image, where four corner points are defined.我有一个图像,其中定义了四个角点。 Now I want to get the pixel values of the region, that is defined by the 4 corners.现在我想获取由 4 个角定义的区域的像素值。 Problem is, although it is a rectangle, it has a "slope", which means neither the two upper corner points nor the lower one are at the same height.问题是,虽然它是一个矩形,但它有一个“斜率”,这意味着两个上角点和下一个角点都不在同一高度。 How can I still solve this issue?我该如何解决这个问题?

I have not found anything for this yet.. I'd appreciate any kind of support: :)我还没有找到任何东西..我会很感激任何形式的支持::)

You can do that with Python/OpenCV by first drawing a white filled polygon on black background as a mask from your four points.您可以使用 Python/OpenCV 来做到这一点,方法是首先在黑色背景上绘制一个白色填充的多边形作为四个点的蒙版。 The use np.where to locate and then print all the points in the image corresponding to the white pixels in the mask.使用 np.where 定位并打印图像中与掩码中的白色像素对应的所有点。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# read image
image = cv2.imread('lena.png')

# create mask with zeros
mask = np.zeros((image.shape), dtype=np.uint8)

# define points (as small diamond shape)
pts = np.array( [[[25,20],[30,25],[25,30],[20,25]]], dtype=np.int32 )
cv2.fillPoly(mask, pts, (255,255,255) )

# get color values
values = image[np.where((mask == (255,255,255)).all(axis=2))]
print(values)

# save mask
cv2.imwrite('diamond_mask.png', mask)

cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()


Mask:面具:

在此处输入图像描述

Results:结果:

 [[108 137 232]
 [104 134 232]
 [108 136 231]
 [106 134 231]
 [109 133 228]
 [108 136 229]
 [109 137 230]
 [110 135 232]
 [103 126 230]
 [112 134 228]
 [114 136 228]
 [111 138 230]
 [110 137 233]
 [103 135 234]
 [103 126 230]
 [101 120 226]
 [108 137 230]
 [112 133 228]
 [114 136 227]
 [115 139 232]
 [112 137 232]
 [105 134 233]
 [102 128 232]
 [ 98 119 226]
 [ 93 105 220]
 [108 139 230]
 [110 137 230]
 [112 135 230]
 [113 135 230]
 [111 138 231]
 [112 139 232]
 [109 134 233]
 [101 128 232]
 [100 120 224]
 [ 90 104 221]
 [ 87  95 211]
 [111 138 229]
 [109 135 231]
 [109 136 230]
 [113 141 233]
 [110 139 233]
 [105 136 234]
 [101 127 232]
 [ 95 117 225]
 [ 90 107 220]
 [110 137 231]
 [110 138 231]
 [107 140 236]
 [110 139 233]
 [104 135 234]
 [105 130 231]
 [ 92 116 227]
 [114 141 234]
 [112 142 235]
 [111 140 235]
 [111 138 234]
 [110 132 232]
 [114 140 234]
 [108 140 233]
 [107 134 233]
 [107 140 235]]

It is not very easy to iterate through a slanted rectangle.遍历一个倾斜的矩形并不容易。 Therefore, what you can do is to rotate the whole image such that the rectangle is parallel to the sides again.因此,您可以做的是旋转整个图像,使矩形再次平行于边。

For this, you can compute the slope of one side as difference in the y coordinate over the difference in the x coordinate of the corners.为此,您可以将一侧的斜率计算为 y 坐标的差异与角的 x 坐标的差异。 The value you get is the slope.你得到的值是斜率。 The arctangent of the slope is the angle to the horizontal.斜率的反正切是与水平面的夹角。 You need to rotate the image with the opposite (negative) of this value.您需要使用该值的相反(负)旋转图像。

To make it more efficient, you can crop a bit the image.为了提高效率,您可以裁剪一点图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM