current position:Home>Fanwai 6 Different operations for image details in Python opencv
Fanwai 6 Different operations for image details in Python opencv
2022-01-30 21:39:32 【Dream eraser】
「 This is my participation 11 The fourth of the yuegengwen challenge 2 God , Check out the activity details :2021 One last more challenge 」
The way this series is written
This series of column writing will adopt the original question and answer writing form , Quickly let you learn OpenCV My primary 、 intermediate 、 Advanced knowledge .
6. stay Python OpenCV Different operations for image details
The goal of this blog is to explain the disassembly of an image , Including the description of image pixels , Acquisition and modification of image attribute information , Image target area ROI Related content , And knowledge of image channels ( Including split channel and merge channel )
These contents are different from numpy
The library is very close , If from the perspective of learning , I suggest you reserve numpy
Related knowledge .
Read and modify the pixel value of the image
In a previous blog post , We have learned how to read an image , Use cv2.imread
Function , And master the two key parameters of the function .
After reading the picture , We can directly use the operation array to obtain the color at any position of the image , Generally, the default order of this color is BGR.
The test code is as follows :
import cv2
import matplotlib.pyplot as plt
src = cv2.imread("./6_test.jpg")
# obtain 100 x 100 Pixel value of the position
print(src[100, 100])
cv2.imshow("src", src)
cv2.waitKey()
Copy code
Here, first get 100 x 100 Pixel value of the position . src[100,100]
You get three values , Respectively corresponding BGR The value of the channel . We mark a pixel on the picture ,rows = 250,cols=470 , Next, modify the above code , See what you get BGR value .
import cv2
import matplotlib.pyplot as plt
src = cv2.imread("./6_test.jpg")
# Note that the format for obtaining pixel values is [cols,rows]
print(src[250, 470])
cv2.imshow("src", src)
cv2.waitKey()
Copy code
The special attention above is , The format for obtaining pixel values is [cols,rows]
, At the top of the list , Line after .
What we got above is BGR value , You can also get only the value of a single channel , The corresponding code is [cols,rows,channel]
, Corresponding to the code section , As shown below :
# Get the blue channel value
print(src[250, 470, 0])
Copy code
The blue channel corresponds to 0, The green channel is 1, The red channel is 2, The above three values are exceeded , There will be the following mistakes :
IndexError: index 3 is out of bounds for axis 2 with size 3
Copy code
At present, if you read the gray image directly , For example, the following code , The values of the three channels are the same .
import cv2
import matplotlib.pyplot as plt
src = cv2.imread("./6_test.jpg", 0)
# Note that the format for obtaining pixel values is [cols,rows]
print(src[250, 470])
cv2.imshow("src", src)
cv2.waitKey()
Copy code
There is also a potential coding problem in this place , If you read a four channel picture , That is, the picture has transparency , The index value of the array can be read 3, That is, the following code is correct .
import cv2
import matplotlib.pyplot as plt
src = cv2.imread("./test.png", -1)
# Note that the format for obtaining pixel values is [cols,rows]
print(src[250, 470, 3])
cv2.imshow("src", src)
cv2.waitKey()
Copy code
src[250, 470, 3]
Successfully read the value of the transparent channel .
We can modify the value for specific pixels , For example, the following code
import cv2
import matplotlib.pyplot as plt
src = cv2.imread("./6_test.jpg")
# Note that the format for obtaining pixel values is [cols,rows]
src[250, 470] = [255, 255, 255]
cv2.imshow("src", src)
cv2.waitKey()
Copy code
Note the position pointed by the red arrow in the figure below , A white bright spot appears , Use this method , Can make a 【 Pepper and salt picture 】.
There is a potential need to pay attention to this place BUG, Number of channels to read pictures , Determines the number of array elements you copy , For example, the following code will report an error .
import cv2
import matplotlib.pyplot as plt
src = cv2.imread("./6_test.jpg")
# Note that the format for obtaining pixel values is [cols,rows]
src[250, 470] = [255, 255, 255, 255]
cv2.imshow("src", src)
cv2.waitKey()
Copy code
The error messages are similar , Prompt that the array dimensions are different .
ValueError: cannot copy sequence with size 4 to array axis with dimension 3
Copy code
Finally, operate the pixels of the image in the above way , It's very time consuming , Because the pixel data of a picture is very large , Generally, it can be used numpy Integrated approach , Don't use this most clumsy way .
Use numpy Get channel value , Note that this method gets scalar , If you want to get all BGR Value , You need to use array.item()
Get... In turn .
import cv2
import numpy as np
src = cv2.imread("./6_test.jpg")
print(src[100, 100])
b = src.item(100, 100, 0)
g = src.item(100, 100, 1)
r = src.item(100, 100, 2)
print(b, g, r)
cv2.imshow("src", src)
cv2.waitKey()
Copy code
If you want to set this value , Use it directly itemset
Function .
src.itemset((100, 100, 0), 200)
print(src[100, 100])
Copy code
You can find any picture for the corresponding test , The operation effect is as follows :
[ 31 68 118]
31 68 118
[200 68 118]
Copy code
OpenCV Analysis of common problems of image attributes in
For an image , In addition to the pixel matrix , There is also a very important content , Is the attribute of the image , These include lines 、 Column 、 passageway 、 data type , Number of pixels 、 Image, shape, etc .
for example , We use it a lot img.shape
To get the shape of the image , In particular , The returned content is the number of rows (rows), Number of columns (cols), And the number of channels (channels), And the return value type is a tuple .
If you read the image , Set tight read grayscale , That will only return the number of rows and columns , Accordingly, through this value, it is easy to determine the type of image you load .
For example, the following code , Read the same picture in different ways , Different shapes of the output image .
import cv2
import numpy as np
# Select a png picture , Different channels can be read
src1 = cv2.imread("./test.png", -1)
src2 = cv2.imread("./test.png", 0)
src3 = cv2.imread("./test.png")
# Four channels , Contains transparent channels
print(src1.shape)
# grayscale
print(src2.shape)
# Three channels
print(src3.shape)
Copy code
The output result can quickly read out whether the image is a color image or a gray image .
(397, 595, 4)
(397, 595)
(397, 595, 3)
Copy code
Use img.size
You can quickly return the total number of pixels in the image , The test code is as follows :
# Select a png picture , Different channels can be read
src1 = cv2.imread("./test.png", -1)
src2 = cv2.imread("./test.png", 0)
src3 = cv2.imread("./test.png")
# Four channels , Contains transparent channels
print(src1.shape)
print(src1.size)
# grayscale
print(src2.shape)
print(src2.size)
# Three channels
print(src3.shape)
print(src3.size)
Copy code
We still have three different reading methods , The number of pixels read is as follows :
(397, 595, 4)
944860
(397, 595)
236215
(397, 595, 3)
708645
Copy code
Be careful , The number of pixels of gray image and color image is different , They had the following relationship before .
The number of pixels in a grayscale image = Row number x Number of columns = 397 x 595 = 236215
The number of pixels in a color image = Row number x Number of columns x The channel number = 944860 ( Four channels )/ 708645( Three channels )
Use img.dtype
Property to get the type of image , As follows :
print(src1.dtype)
Copy code
The value read here , It's all the same uint8
Express 8 Bit image , Here you can remember that as long as it is uint8
Format , That corresponds to BGR The range of values is in [0,255]
Between .
When operating the above attribute values , The following will appear BUG, The BUG The general solution is to check whether the picture is read normally , You need to pay special attention to :
AttributeError: 'NoneType' object has no attribute 'shape'
Copy code
print(src1.dtype)
Copy code
The value read here , It's all the same uint8
Express 8 Bit image , Here you can remember that as long as it is uint8
Format , That corresponds to BGR The range of values is in [0,255]
Between .
When operating the above attribute values , The following will appear BUG, The BUG The general solution is to check whether the picture is read normally , You need to pay special attention to :
AttributeError: 'NoneType' object has no attribute 'shape'
Copy code
copyright notice
author[Dream eraser],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302139306656.html
The sidebar is recommended
- Introduction to python (IV) dynamic web page analysis and capture
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
guess what you like
-
This is what you should do to quickly create a list in Python
-
On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
-
Python OpenCV image area contour mark, which can be used to frame various small notes
-
How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
-
Initial Python tuple
-
Introduction to Python urllib module
-
Advanced Python Basics: from functions to advanced magic methods
-
Python Foundation: data structure summary
-
Python Basics: from variables to exception handling
-
Python notes (22): time module and calendar module
Random recommended
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
- Parameter passing of Python function
- Stroke tuple in Python
- Talk about ordinary functions and higher-order functions in Python
- [Python data acquisition] page image crawling and saving
- [Python data collection] selenium automated test framework
- Talk about function passing and other supplements in Python
- Python programming simulation poker game
- leetcode 160. Intersection of Two Linked Lists (python)
- Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
- Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems