preface
For the conversion of color space, we will introduce you through HSV actual combat. As for the conversion and application of other color Spaces, I will learn other knowledge separately in the follow-up. Starting with this article, we will study the geometry transformation of OpenCV.
For example, in the field of image processing, we will often encounter image scaling, rotation and other operations, especially for users who have used PS experience, for these operations must be handy, but in fact, the underlying code is to explain the knowledge behind. In this article we will introduce scaling.
The zoom
In OpenCV, it gives us a scaling function called cv2.resize(), which is defined as follows:
def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None) :
Copy the code
SRC: represents the original image that needs to be scaled
Dsize: The size of a scaled image
Fx: represents the horizontal scaling ratio
Fy: indicates the scale in the vertical direction
Interpolation: Interpolation method
The interpolation values are shown in the table:
type | instructions |
---|---|
cv2.INTER_LINEAR | Bilinear interpolation (default) |
cv2.INTER_NEARSET | Nearest interpolation |
cv2.INTER_CUBIC | Cubic spline interpolation. First, the cubic spline fitting of the 4*4 nearest neighbor region near the source image is carried out, and then the cubic spline value corresponding to the target pixel is taken as the value of the corresponding pixel point of the target image |
cv2.INTER_AREA | Regional interpolation: the current pixel is sampled according to the pixels in the surrounding area of the current pixel, which is similar to the nearest interpolation method |
cv2.INTER_LANCZ0S4 | A Lanczos interpolation scheme using 8*8 nearest neighbors |
cv2.INTER_LINEAR_EXACT | Bitwise accurate bilinear interpolation |
cv2.INTER_MAX | Differential encoding mask |
cv2.WARP_FILL_OUTLIERS | Fills all pixels in the target image. If some of them correspond to singularities (outliers) in the source image, they are set to 0 |
cv2.WARP_INVERSE_MAP | Scale, inverse transformation, for example, polar transformation. If flag is not set, convert: DST (ø,p)= SRC (x,y); If flag is set, convert: DST (x,y)= SRC (ø p) |
In cv2.resize(), the size of the target image can be specified by either “dsize” or “fx,fy”.
When you specify dsize, whether you specify fx,fy or not, the dsize parameter determines the size of the target image, that is, dsize has the highest priority. The specific mathematical formula is as follows:
Width = (double) dszie. Width/SRC cols
Height = (double) dszie. Height/SRC. The rows
Note that the first argument to dsize is the number of columns, and the second argument is the number of rows, as opposed to shape.
When you specify the target image size with fx,fy, the math is as follows:
dsize=Size(round(fxsrc.cols),round(fysrc.rows))
As for the final parameter interpolation, it refers to assigning values to pixels that cannot be directly obtained by mapping during geometric processing of the image. For example, if you want to enlarge an image by 2 times in a certain size area, some images may be too small to fit some pixels missing, and some may be too large to fit some pixels missing. For these pixels, interpolation determines how to determine their values.
Dsize enables scaling
Now that we know all the parameters of the resize function in OpenCV, let’s implement a simple image scaling as follows:
import cv2
img = cv2.imread("4.jpg")
rows,cols=img.shape[:2]
size=(int(cols*2),int(rows*1))
result = cv2.resize(img, size)
cv2.imshow("img", img)
print(img.shape)
cv2.imshow("result", result)
print(result.shape)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
Here, we first get the image’s length and width pixels using img.shape, and then enlarge the rows unchanged and the columns by 2 times. After running, we get the following image.
Fx,fy scale
We used dszie parameters to scale above, now we use FX,fy parameters to scale as shown below:
import cv2
img = cv2.imread("4.jpg")
result = cv2.resize(img, None,fx=2,fy=1)
cv2.imshow("img", img)
print(img.shape)
cv2.imshow("result", result)
print(result.shape)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
This code implements the same effect as above, and the results are not shown. As you can see, fx and FY code will be much more concise.