官术网_书友最值得收藏!

Image scaling

In this section, we will discuss about resizing an image. This is one of the most common operations in computer vision. We can resize an image using a scaling factor, or we can resize it to a particular size. Let's see how to do that:

img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_LINEAR)
cv2.imshow('Scaling - Linear Interpolation', img_scaled) img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_CUBIC)
cv2.imshow('Scaling - Cubic Interpolation', img_scaled) img_scaled = cv2.resize(img,(450, 400), interpolation = cv2.INTER_AREA)
cv2.imshow('Scaling - Skewed Size', img_scaled) cv2.waitKey()

What just happened?

Whenever we resize an image, there are multiple ways to fill in the pixel values. When we are enlarging an image, we need to fill up the pixel values in between pixel locations. When we are shrinking an image, we need to take the best representative value. When we are scaling by a non-integer value, we need to interpolate values appropriately, so that the quality of the image is maintained. There are multiple ways to do interpolation. If we are enlarging an image, it's preferable to use linear or cubic interpolation. If we are shrinking an image, it's preferable to use the area-based interpolation. Cubic interpolation is computationally more complex, and hence slower than linear interpolation. But the quality of the resulting image will be higher.

OpenCV provides a function called resize to achieve image scaling. If you don't specify a size (by using None), then it expects the X and Y scaling factors. In our example, the image will be enlarged by a factor of 1.2. If we do the same enlargement using cubic interpolation, we can see that the quality improves, as seen in the following figure. The following screenshot shows what linear interpolation looks like:

What just happened?

Here is the corresponding cubic interpolation:

What just happened?

If we want to resize it to a particular size, we can use the format shown in the last resize instance. We can basically skew the image and resize it to whatever size we want. The output will look something like the following:

What just happened?
主站蜘蛛池模板: 佛坪县| 崇文区| 林芝县| 和硕县| 洞口县| 阳曲县| 彩票| 大余县| 惠安县| 刚察县| 萍乡市| 金堂县| 大城县| 虎林市| 林周县| 正定县| 望城县| 砚山县| 新兴县| 孝昌县| 库车县| 鹰潭市| 湖北省| 荣成市| 江西省| 库伦旗| 枣强县| 沅陵县| 莒南县| 西乌| 卓资县| 仁化县| 平罗县| 通渭县| 盖州市| 丁青县| 巩留县| 龙川县| 乳山市| 邛崃市| 博客|