current position：Home>Optimization iteration of nearest neighbor interpolation and bilinear interpolation algorithm for Python OpenCV image
Optimization iteration of nearest neighbor interpolation and bilinear interpolation algorithm for Python OpenCV image
2022-01-30 05:10:13 【Dream eraser】
Little knowledge , Great challenge ！ This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .
Python OpenCV 365 Day study plan , Go into the field of image with the eraser . This blog is the third in this series 43 piece .
First make up for a small hole in yesterday's article , After the last piece of code is implemented , It is found that there are many jagged edges on the image after running .
While wondering , There must be something wrong with the details of the code , When reviewing the code, I found a problem , Note the following codes ：
dst[dst_y, dst_x, n] = (1-u)*(1-v)*src[j, i, n]+u*(1-v) * src[j+1, i, n] + (1-u)*v*src[j, i+1, n] + u*v*src[j+1, i+1, n] Copy code
Comparison with formula , You can compare this detailed work by yourself . f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1)
The problem lies in the rows and columns of the image , Code
u*(1-v) * src[j+1, i, n] + (1-u)*v*src[j, i+1, n] I wrote this part backwards ~ embarrassed
Change to the following code , Get it done , As like as two peas. .
dst[dst_y, dst_x, n] = (1-u)*(1-v)*src[j, i, n]+v*(1-u) * src[j+1, i, n] + (1-v)*u*src[j, i+1, n] + u*v*src[j+1, i+1, n] Copy code
Let's talk about algorithm optimization , We're still there This blog Dug a small hole , The final result of nearest neighbor interpolation algorithm is not satisfactory , When the image is magnified, there is a strong sawtooth .
First migrate the previous code , Modify to the following format ：
import cv2 as cv import numpy as np def nearest_demo(src, multiple_y, multiple_x): src_y, src_x, src_c = src.shape tar_x, tar_y, tar_c = src_x*multiple_x, src_y*multiple_y, src_c # Generate a black target image tar_img = np.zeros((tar_y, tar_x, tar_c), dtype=np.uint8) print(tar_img.shape) # The value of the rendered pixel # Be careful y It's height ,x It's the width for y in range(tar_y-1): for x in range(tar_x-1): # Calculate the new coordinates (x,y) Which value is the coordinate in the source diagram src_y = round(y*src_y/tar_y) src_x = round(x*src_x/tar_x) tar_img[y, x] = src[src_y, src_x] return tar_img src = cv.imread("./ttt.png") print(src.shape) cv.imshow("src", src) # dsize = (cols,rows) chinese ,( Width , Height ) dst = cv.resize(src, (src.shape*2, src.shape * 2), interpolation=cv.INTER_NEAREST) cv.imshow("dst", dst) new_dst = nearest_demo(src, 2, 2) cv.imshow("new_dst", new_dst) cv.waitKey(0) cv.destroyAllWindows() Copy code
The first part of the optimization is about the center point , This part is serious , The eraser found a lot of information , They didn't explain everything very clearly , Basically, I skipped the key points , I'm paraphrasing it , If you have a good explanation , Please provide it to me in the comments area , The point is that 0.5 Pixel problem .
If the above code wants to implement and OpenCV Provide the same effect as the built-in function , Key modifications are as follows ：
srcy = round(y*src_y/tar_y) srcx = round(x*src_x/tar_x) # Revised as follows srcy = round((y+0.5)*src_y/tar_y-0.5) srcx = round((x+0.5)*src_x/tar_x-0.5) Copy code
It was in this place that I was directly ignorant , Some blogs wrote
srcX=dstX* (srcWidth/dstWidth)+0.5*(srcWidth/dstWidth-1) It's equivalent to adding... To the original floating-point coordinates
But there is no other explanation for this place , Some bloggers will compare two images and say that the geometric center of the source image and the target image should be aligned , But I just drew two pictures and some simple text descriptions , The excerpt is as follows ：
Suppose the source image is
3x3, The coordinates of the center point of this image are
(1,1), The target image is
9x9, The coordinates of the center point are
(4,4), The center point alignment should refer to
(4,4) Align points to
(1,1) spot , But according to
srcx = round(x*src_x/tar_x) And
srcy = round(y*src_y/tar_y) Formula calculation , The coordinates of the center point obtained are
(1.333,1.333) Not at all
(1,1), Lower right of the whole image , Now you need to align .
Let's make a simple calculation . The center point alignment assumes that the source image does not move in advance , Finally, there should be the following formula .
The source image
src Center point coordinates
, Target image
dst Center point coordinates
Bring the above values into the formula ：
The formula is converted into
Finally, we can get K The value of is
therefore K In fact, it is equal to
Corresponding to the code , It becomes the following ：
# Before the change src_y = round(dst_y*src_height/tar_height) src_x = round(dst_x*src_width/tar_width) # After modification src_y = round(dst_y*src_height/tar_height+1/2*(src_height/tar_height-1)) src_x = round(dst_x*src_width/tar_width+1/2*(src_height/tar_height-1)) Copy code
Actually, you're here for a while , And you get the final result .
# After modification src_y = round((dst_y+0.5)*src_height/tar_height-0.5) src_x = round((dst_x+0.5)*src_width/tar_width-0.5) Copy code
After the modification , The running feeling is better than the built-in effect of the system , Ha ha ha .
The same optimization is copied into the bilinear interpolation algorithm , I can compare the effect of running .
About running speed , I also found some information , However, the current basic knowledge is not enough , Let's put it on hold for now , This series has 100 When , We're talking about .
I didn't think of , One 0.5 The small problem of pixels is so laborious .
I hope today's 1 You get something in an hour , I'll see you on our next blog ~
author[Dream eraser],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Python collects and monitors system data -- psutil
- Python chat room (Tkinter writing interface, streaming, socket to realize private chat, group chat, check chat records, Mysql to store data)
- Learning this Python library can reduce at least 100 lines of code
- leetcode 67. Add Binary（python）
- Regular re parameter replacement of Python 3 interface automation test framework
- V. pandas based on Python
- Only 15 lines of code is needed for face detection! (using Python and openCV)
- [Python crawler Sao operation] you can crawl Sirius cinema movies without paying
- leetcode 69. Sqrt(x)（python）
- Teach you to read the source code of Cpython (I)
guess what you like
Snowball learning started in the fourth quarter of Python. One needs three meals. I have a new understanding of Python functional programming, process-oriented, object-oriented and functional
leetcode 88. Merge Sorted Array（python）
Don't you know more about a python library before the end of 2021?
Python crawler web page parsing artifact XPath quick start teaching!!!
Use Python and OpenCV to watermark the image
String and related methods of Python data type introduction
Heapq module of Python module
Introduction to beautiful soup of Python crawler weapon, detailed explanation, actual combat summary!!!
Event loop of Python collaboration series
Django docking pin login system
- [recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos
- You used to know Python advanced
- Pyinstaller package Python project
- 2021 IEEE programming language rankings: Python tops the list!
- Implementation of Python automatic test control
- Python advanced: [Baidu translation reverse] graphic and video teaching!!!
- Do you know the fuzzy semantics in Python syntax?
- [Python from introduction to mastery] (XXVII) learn more about pilot!
- Playing excel office automation with Python
- Some applications of heapq module of Python module
- Python and go languages are so popular, which is more suitable for you?
- Python practical skills task segmentation
- Python simulated Login, numpy module, python simulated epidemic spread
- Python opencv contour discovery function based on image edge extraction
- Application of Hoff circle detection in Python opencv
- Python reptile test ox knife (I)
- Day 1: learn the Django framework of Python development
- django -- minio_ S3 file storage service
- [algorithm learning] 02.03 Delete intermediate nodes (Java / C / C + + / Python / go)
- Similarities and differences of five pandas combinatorial functions
- Learning in Python + opencv -- extracting corners
- Python beginner's eighth day ()
- Necessary knowledge of Python: take you to learn regular expressions from zero
- Get your girlfriend's chat records with Python and solve the paranoia with one move
- My new book "Python 3 web crawler development practice (Second Edition)" has been recommended by the father of Python!
- From zero to familiarity, it will take you to master the use of Python len() function
- Python type hint type annotation guide
- leetcode 108. Convert Sorted Array to Binary Search Tree（python）
- For the geometric transformation of Python OpenCV image, let's first talk about the extraordinary resize function
- leetcode 701. Insert into a Binary Search Tree （python）
- For another 3 days, I sorted out 80 Python datetime examples, which must be collected!
- Python crawler actual combat | using multithreading to crawl lol HD Wallpaper
- Complete a python game in 28 minutes, "customer service play over the president card"
- The universal Python praise machine (commonly known as the brushing machine) in the whole network. Do you want to know the principle? After reading this article, you can write one yourself
- How does Python compare file differences between two paths
- Common OS operations for Python
- [Python data structure series] linear table - explanation of knowledge points + code implementation
- How Python parses web pages using BS4
- How do Python Network requests pass parameters
- Python core programming - decorator