当前位置:网站首页>Hegong sky team vision training Day6 - traditional vision, image processing
Hegong sky team vision training Day6 - traditional vision, image processing
2022-07-26 08:46:00 【Electric power department of University of Technology】
Catalog
1. Grayscale processing when reading
3、 ... and 、 Two valued ( Thresholding ) operation
5、 ... and 、 Ying Wei Da Jetson TX2
Learning goals :
- Preprocess the image
- Convert to grayscale
- Two valued ( Thresholding ) operation
- Open close operation
- understand NVIDIA Jetson TX2
Learning content :
- The image processing
- understand tx2
Learning time :
- 2022 year 7 month 23 Japan
Learning output :
One 、 Image preprocessing
The theoretical basis is :g(i,j)=α∗f(i,j)+β α Used to adjust the contrast of the image ,β Used to adjust image brightness .
1.zeros function
The functionality : Create a zero matrix of the specified size and type .
Constructors :
cv::Mat::zeros( int rows,int cols,int type) % The first type of constructor
Parameters 1: Specify the number of lines
Parameters 2: Specify the number of columns
Parameters 3: Specify the type
cv::Mat::zeros( Size size,int type) % The second constructor
Parameters 1: Specify dimensions
Parameters 2: Specify the type
2.at function
The functionality : Operate on the specified array elements .
Constructors :
//at There are many refactoring functions , Several commonly used constructors are as follows :
// For single channel
cv::Mat::at< identifier >(int i0);// For a one-dimensional array , Get the corresponding element directly
cv::Mat::at< identifier >(int row, int col);// For two dimensional arrays , Get the elements on the corresponding row and column
// The type of identifier is related to the type of image :
CV_8U ——> Mat.at<uchar>(y,x).
CV_8S ——> Mat.at<schar>(y,x).
CV_16U ——> Mat.at<ushort>(y,x).
CV_16S ——> Mat.at<short>(y,x).
CV_32S ——> Mat.at<int>(y,x).
CV_32F ——> Mat.at<float>(y,x).
CV_64F ——> Mat.at<double>(y,x).
// For multichannel , Generally refers to three channels
cv::Mat::at< identifier >(int row, int col)[i];//i Is the number of channels
// The identifier is :
Vec<uchar, 3> Vec3b;
Vec<short, 3> Vec3s;
Vec<ushort, 3> Vec3w;
Vec<int, 3> Vec3i;
Vec<float, 3> Vec3f;
Vec<double, 3> Vec3d;
3.saturate_cast function
The functionality : This is a cast function , That is to convert data from the original type to another new type , Take advantage of this feature , It can be used as a specific saturation function .
4. Code as a whole
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int pos = 0;
float alpha = 1;
float beta = 0;
Mat src, dst, dst1;
void set_brightness(int,void*) {
src = imread("test.jpg");
//cvtColor(src, src, COLOR_BGR2GRAY);
int height = src.rows;
int width = src.cols;
beta = pos;
dst = Mat::zeros(src.size(), src.type());
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
if (src.channels() == 3) {// If the read image is rgb Images
float b = src.at<Vec3b>(i, j)[0];
float g = src.at<Vec3b>(i, j)[1];
float r = src.at<Vec3b>(i, j)[2];
dst.at<Vec3b>(i, j)[0] = saturate_cast<uchar> (b * alpha + beta);
dst.at<Vec3b>(i, j)[1] = saturate_cast<uchar> (g * alpha + beta);
dst.at<Vec3b>(i, j)[2] = saturate_cast<uchar> (r * alpha + beta);
}
else if (src.channels() == 1) { If it's a grayscale image
float m = src.at<uchar>(i, j);
dst.at<uchar>(i, j) = saturate_cast<uchar> (m * alpha + beta);
}
}
imshow("out", dst);
}
int main() {
namedWindow("out", 1);// create a window
createTrackbar("Brightness", "out", &pos, 100, set_brightness);// Create a progress bar
set_brightness(0,0);// Call the set brightness function
imshow("input", src);
waitKey(0);
}
It can be adjusted .
Two 、 Convert to grayscale
1. Grayscale processing when reading
def img2Gray(filePath):
image = cv.imread(filePath)
cv.imshow("sourcePic",image)
gray1 = cv.imread(filePath,cv.IMREAD_GRAYSCALE)
cv.imshow("read2gray",gray1)
cv.waitKey(0)
cv.destroyAllWindows()
2. call cvtColor graying
def img2Gray(filePath):
image = cv.imread(filePath)
cv.imshow("sourcePic",image)
gray2 = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("cvtColor2gray", gray2)
cv.waitKey(0)
cv.destroyAllWindows()
3. Average method :
def img2Gray(filePath):
image = cv.imread(filePath)
cv.imshow("sourcePic",image)
# Average method
h, w = image.shape[:2]
gray3 = np.zeros((h, w), dtype=np.uint8)
for i in range(h):
for j in range(w):
gray3[i, j] = (int(image[i, j][0]) + int(image[i, j][1]) + int(image[i, j][2])) / 3
cv.imshow("meanGray", gray3)
4. Maximum method
def img2Gray(filePath):
image = cv.imread(filePath)
cv.imshow("sourcePic",image)
# Maximum method
h, w = image.shape[:2]
gray4 = np.zeros((h, w), dtype=np.uint8) # Create a h That's ok w The two-dimensional dimension of the column list
for i in range(h):
for j in range(w):
gray4[i, j] = max(image[i, j][0], image[i, j][1], image[i, j][2])
cv.imshow("maxGray",gray4)
5. Component method
def img2Gray(filePath):
image = cv.imread(filePath)
cv.imshow("sourcePic",image)
# Component method :
gray6 = cv.imread(filePath, cv.IMREAD_COLOR)
for i in range(gray6.shape[0]):
for j in range(gray6.shape[1]):
gray6[i, j] = gray6[i, j, 0]
cv.imshow("componentGray",gray6)
6. Weighted average
def img2Gray(filePath):
image = cv.imread(filePath)
cv.imshow("sourcePic",image)
# Weighted average
h, w = image.shape[:2]
gray5= np.zeros((h, w), dtype=np.uint8)
for i in range(h):
for j in range(w):
# Y = 0.3R + 0.59G + 0.11B
# adopt cv Format open picture , The pixel format is BGR
gray5[i, j] = 0.3 * image[i, j][2] + 0.11 * image[i, j][0] + 0.59 * image[i, j][1]
cv.imshow("weightedGray",gray5)
3、 ... and 、 Two valued ( Thresholding ) operation
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
Mat img;
img = imread("C:\\Users\\86139\\Desktop\\target1.png");
if (img.empty())
{
printf("could not load the picture...");
}
// Turn to binary graph
threshold(img, img, 100, 255, THRESH_BINARY);
imshow("Binarization", img);
waitKey(0);
return 0;
}
The image is composed of a matrix , Of each point in the matrix RGB It's not worth the same , The colors are different , The final whole presented to us is a color image . So-called ” binarization “ Is to put the of each point in the matrix RGB value (0,0,0)[ black ] perhaps (255,255,255)[ white ] Simple threshold is to select a global threshold , Then divide the whole image into binary images that are either black or white , If the gray value is greater than the threshold, it is assigned 255 Instead of 0.
ret,mask = cv2.threshold(img2gray,175,255,cv2.THRESH_BINAR)
The return value is one : threshold ,(Otsu‘s Binarization will use )
Return value two : After processing the image
Parameter one : Initial image
Parameter two : Set threshold
Parameter 3 : When the image pixel setting exceeds our set threshold, it is set to 255
Parameter 4 : Set the binarization type
The effect of binarization depends on whether channel subtraction or other operations are performed in the previous step and threshold Deviation caused by threshold setting in function . Next, we will directly use the image after graying to binarize .
GaussianBlur(gray, gray, Size(3, 3), 1);
threshold(gray, dst, 100, 255, THRESH_BINARY_INV);
imshow("dst", dst);
Four 、 Open close operation
Knowledge points supplement :
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
Mat img;
img = imread("C:\\Users\\86139\\Desktop\\target2.png");
if (img.empty())
{
printf("could not load the picture...");
}
Mat element;
Mat dstImage;
element = getStructuringElement(MORPH_RECT, Size(4, 4)); //Size Select the precision to process , The bigger, the more
morphologyEx(img, dstImage, MORPH_OPEN, element);
imshow("open", dstImage);
waitKey(0);
return 0;
}
Processing results :
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
Mat img;
img = imread("C:\\Users\\86139\\Desktop\\target2.png");
if (img.empty())
{
printf("could not load the picture...");
}
Mat element;
Mat dstImage;
element = getStructuringElement(MORPH_RECT, Size(4, 4)); //Size Select the precision to process , The bigger, the more
morphologyEx(img, dstImage, MORPH_CLOSE, element);
imshow("close", dstImage);
waitKey(0);
return 0;
}
Processing results :
Open close operation :
// Ditto above , I won't repeat //
element = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(dst, dst, MORPH_CLOSE, element);
imshow("morphology", dst);
waitKey(0);
return 0;
}
Processing results :
In fact, there is another binarization operation for all processing results. I won't repeat it .
5、 ... and 、 Ying Wei Da Jetson TX2
Jetson TX2 yes NIVDIA Aim at AI in Jetson TK1 and TX1 Upgrade after launch
TX2 Of GPU and CPU All have been upgraded , Memory increased to 8GB、 Storage has increased to 32GB, Support Wifi And the bluetooth , Codec support H.265, The size is also small .
According to the NVIDIA The official introduction ,Jetson TX2 Two operating modes are provided : One is MAX Q, The energy efficiency ratio can reach the highest , It's from the previous generation TX1 Of 2 times , Power consumption is 7.5W following ; The other is MAX P, Performance can be highest , The energy efficiency ratio can also be the same as that of the previous generation 2 times , The power consumption is 15W following .
边栏推荐
- Using the primitive root of module m to judge and solve
- P3743 kotori的设备
- [GUI] swing package (window, pop-up window, label, panel, button, list, text box)
- 请问flink sql client 在sink表,有什么办法增大写出速率吗。通过sink表的同步时
- Run file command
- After MySQL 8 OCP (1z0-908), hand in your homework
- Why reserve a capacitor station on the clock output?
- 请问现在flinkcdc支持sqlserver实例名方式连接吗?
- One click deployment of lamp and LNMP scripts is worth having
- Flitter imitates wechat long press pop-up copy recall paste collection and other custom customization
猜你喜欢
6、 Pinda general permission system__ pd-tools-log
node的js文件引入
基于C语言的哈夫曼转化软件
keepalived双机热备
【数据库 】GBase 8a MPP Cluster V95 安装和卸载
Memory management - dynamic partition allocation simulation
node-v下载与应用、ES6模块导入与导出
File management file system based on C #
Grid segmentation
[GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)
随机推荐
Huffman transformation software based on C language
Neo eco technology monthly | help developers play smart contracts
Logic of data warehouse zipper table
Solve the problem of C # calling form controls across threads
Flutter compilation fails
Alphabetic string
Run file command
Using the primitive root of module m to judge and solve
[freeswitch development practice] user defined module creation and use
Kotlin data type
Registration of finite element learning knowledge points
Implementation of Prometheus web authentication and alarm
Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field
Oracle 19C OCP 1z0-082 certification examination question bank (19-23)
What are the contents of Oracle OCP and MySQL OCP certification exams?
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
TypeScript版Snowflake主键生成器
CIS 2020 - alternative skills against cloud WAF (pyn3rd)
Cadence(十)走线技巧与注意事项
Grid segmentation