当前位置:网站首页>Hegong sky team vision training Day2 - traditional vision, opencv basic operation
Hegong sky team vision training Day2 - traditional vision, opencv basic operation
2022-07-24 07:50:00 【Electric power department of University of Technology】
Catalog
1、OpenCV Library and Matlab、halcon The difference between ?
2、 After display , No use destroyWindow() What's the difference? ?
3、png Picture format and jpg What is the difference between picture formats ?
Two 、 Exercises (C++ Realization )
1. Display two pictures with different resolutions at the same time , Compare their size
2. Use Opencv, Test the resolution and frame rate of your computer camera
3、 ... and 、 Exercises (python Realization )
1. Display two pictures with different resolutions at the same time , Compare their size
2. Use Opencv, Test the resolution and frame rate of your computer camera
Learning goals
- understand Opencv The development history 、Opencv Version difference
Master the reading of pictures and videos 、 Show 、 Save method
master Opencv Basic drawing operations
Learning content
- build opencv development environment
- master opencv Simple operation
Learning time
- 2022 year 7 month 19 Japan
Learning output
The following is the overall learning process ( The code to build , Question answering process ), In terms of code implementation, I start from C++ Language and python Language starts from two directions , On the whole, the former is more difficult to achieve than the latter .
One 、 Thinking questions
1、OpenCV Library and Matlab、halcon The difference between ?
Halcon And OpenCV All belong to the function library , All provide interfaces accessed by multiple programming languages , and Matlab It is a complete integrated development environment , Including the editor 、 function library 、 also Matlab Language . For example, use Matlab Can directly Debug, But with Halcon and OpenCV Relevant code development needs cooperation IDE.Opencv: They are good at recognition , For example, face recognition 、 Video recognition, etc ;Halcon: In dimension measurement , Feeling Halcon Well done , Its calibration package is better , Measurement can directly come out the size , It's more convenient .
2、 After display , No use destroyWindow() What's the difference? ?
destroyWindow() Used to close a specific window , Fill in the name of the window in brackets , The type is string.
3、png Picture format and jpg What is the difference between picture formats ?
1.jpg It belongs to a lossy compressed picture file , It is a popular image file format in the network ,jpg The image file can be compressed to the smallest format ;png It belongs to lossless compressed picture file ,PNG-8 Support transparency , However, translucency is not supported , therefore PS When storing, there will be miscellaneous edge colors .PNG-24 Support transparency , Support translucency , The size of the stored file will be larger than PNG-8 Big .
2.jpg Format pictures can be highly compressed at the same time , It can show very vivid and rich images , But with the increase of compression , The picture quality will gradually decline ; however png The picture is different , Small volume , Less space , And jpg Compared with the format ,png Pictures without losing picture data , You can get the pictures you need more quickly , And the quality of the picture will not decline .
3.png Format pictures can be edited , For example, the text style in the picture , Lines, etc , It can be used ps Software changes such as ;jpg The picture in format cannot be edited normally .png And jpg Picture comparison png The format of the picture is larger .
Two 、 Exercises (C++ Realization )
1. Display two pictures with different resolutions at the same time , Compare their size
Very regret ,OpenCV There doesn't seem to be anything like that in China Matlab Similar functions in , So we need to customize the function , The core idea is to display the image as needed , Create a new big image , Use ROI Operate to copy the image to be displayed into the new large image .
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat combineImages(vector<Mat> imgs,int col,int row,bool hasMargin);// Function declaration
int main(){
vector<Mat> imgs(2);
imgs[0] = imread("D:/xxxxxx");
imgs[1] = imread("D:/xxxxxx");
Mat m = combineImages(imgs, 2, 2, false);
namedWindow("test",0);
imshow("test", m);
//imwrite("D:/research/Test1/rawimage/0.jpg", m);
waitKey(0);
return 0;
}
Mat combineImages(vector<Mat> imgs,
int col,
int row,
bool hasMargin){
int imgAmount = imgs.size();
int width = imgs[0].cols;
int height = imgs[0].rows;
int newWidth, newHeight;
if (!hasMargin){
newWidth = col*imgs[0].cols;
newHeight = row*imgs[0].rows;
}
else{
newWidth = (col + 1) * 20 + col*width;
newHeight = (row + 1) * 20 + row*height;
}
Mat newImage(newHeight, newWidth, CV_8UC3, Scalar(255, 255, 255));
int x, y,imgCount;
if (hasMargin){
imgCount = 0;
x = 0; y = 0;
while (imgCount < imgAmount){
Mat imageROI = newImage(Rect(x*width + (x + 1) * 20, y*height + (y + 1) * 20, width, height));// Create regions of interest
imgs[imgCount].copyTo(imageROI);
imgCount++;
if (x == (col - 1)){
x = 0;
y++;
}
else{
x++;
}
}
}
else{
imgCount = 0;
x = 0; y = 0;
while (imgCount < imgAmount){
Mat imageROI = newImage(Rect(x*width, y*height, width, height));
imgs[imgCount].copyTo(imageROI);
imgCount++;
if (x == (col - 1)){
x = 0;
y++;
}
else{
x++;
}
}
}
return newImage;
};2. Use Opencv, Test the resolution and frame rate of your computer camera
It should be noted that opencv4 Identifier not defined in CV_CAP_PROP_FPS;CV_CAP_PROP_FRAME_COUNT;CV_CAP_PROP_POS_FRAMES problem , because opencv Version update too fast ,opencv4 Many identifiers have changed in , Make the following changes to the following three identifiers :
CV_CAP_PROP_FPS -> CAP_PROP_FPS
CV_CAP_PROP_FRAME_COUNT -> CAP_PROP_FRAME_COUNT
CV_CAP_PROP_POS_FRAMES -> CAP_PROP_POS_FRAMES
And if not defined before cv In terms of namespace (using namespace cv;) It is necessary to use cv::CAP_PROP_FRAME_COUNT This format .
#include <stdlib.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc, const char** argv)
{
cv::Mat frame;
// You can input video streams from the camera or play video files directly
cv::VideoCapture capture(0);
// cv::VideoCapture capture("vedio1.avi");
double fps;
char string[10];
cv::namedWindow("Camera FPS");
double t = 0;
while (1)
{
t = (double)cv::getTickCount();
if (cv::waitKey(1) == 27) { break; } //#if (cv::waitKey(100) == 27) { break; }
if (capture.isOpened())
{
capture >> frame;
t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
fps = 1.0 / t;
sprintf_s(string, "%.2f", fps);
std::string fpsString("FPS:");
fpsString += string;
printf("fps: %.2f width:%d height:%d fps:%.2f\n", fps, frame.cols, frame.rows, capture.get(cv::CAP_PROP_FPS));
cv::putText(frame, // Image matrix
fpsString,
cv::Point(5, 20),
cv::FONT_HERSHEY_SIMPLEX,
0.5,
cv::Scalar(0, 0, 0));
cv::imshow("Camera FPS", frame);
}
else
{
std::cout << "No Camera Input!" << std::endl;
break;
}
}
}

3. Take an image of yourself from the outside with a computer camera , Add circle ( Or other graphics ) Code yourself , At the same time, you can change the resolution by pressing the key , And add time in the lower right corner of the picture
The original title is to code with pictures , Press the key to change the resolution, but my code always has bug, So I did a facial treatment , Set up ROI after , You can operate the area as a whole . This code will a region of interest A Assign a value to a variable B after , This variable can be B Assign to another area C, So as to achieve in the area C Copy area within A Purpose .
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ret,frame = cap.read()
cv2.imshow('cap',frame)
cv2.waitKey()
cv2.destroyAllWindows()
lena=cv2.imread("lena.bmp",0)
h,w=lena.shape
mask=np.zeros((h,w),dtype=np.uint8)
mask[220:400,250:350]=1
key=np.random.randint(0,256,size=[h,w],dtype=np.uint8)
lenaXorKey=cv2.bitwise_xor(lena,key)
encryptFace=cv2.bitwise_and(lenaXorKey,mask*255)
noFace1=cv2.bitwise_and(lena,(1-mask)*255)
maskFace=encryptFace+noFace1
extractFace=cv2.bitwise_and(extractOriginal,mask*255)
noFace2=cv2.bitwise_and(maskFace,(1-mask)*255)
extractLena=noFace2+extractFace
cv2.imshow("lena",lena)
cv2.imshow("mask",mask*255)
cv2.imshow("1-mask",(1-mask)*255)
cv2.imshow("key",key)
cv2.imshow("lenaXorKey",lenaXorKey)
cv2.imshow("encryptFace",encryptFace)
cv2.imshow("noFace1",noFace1)
cv2.imshow("maskFace",maskFace)
cv2.imshow("extractOriginal",extractOriginal)
cv2.imshow("extractFace",extractFace)
cv2.imshow("noFace2",noFace2)
cv2.imshow("extractLena",extractLena)
cv2.waitKey()
cv2.destroyAllWindows()
3、 ... and 、 Exercises (python Realization )
1. Display two pictures with different resolutions at the same time , Compare their size
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
int main() {
Mat img = imread(" Your path 1" , 1);
Mat img1 = imread(" Your path 2" , 1);
printf(" chart 1 The resolution is :%dx%d\n", img.cols, img.rows);
printf(" chart 2 The resolution is :%dx%d", img1.cols, img1.rows);
return 0;
}2. Use Opencv, Test the resolution and frame rate of your computer camera
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
int main() {
VideoCapture capture;
Mat frame;
frame= capture.open(" Your path ");
int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
namedWindow("output", WINDOW_AUTOSIZE);
while (capture.read(frame))
{
imshow("output", frame);
char ch = waitKey(30);
if(ch == 27){
break;
}
}
printf("FPS:%d\n", fps);
printf(" This resolution is %dx%d",width ,height);
capture.release();
return 0;
}3. Take an image of yourself from the outside with a computer camera , Add circle ( Or other graphics ) Code yourself , At the same time, you can change the resolution by pressing the key , And add time in the lower right corner of the picture
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main() {
double scale_up_x = 1.0;
double scale_up_y = 1.0;
int i = 1;
Mat scaled_f_up, scaled_f_down;
while(i){
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::stringstream ss;
ss << std::put_time(std::localtime(&t), "%Y-%m-%d-%H:%M:%S");
std::string str_time = ss.str();
Mat pic = imread("/Users/milkman/Desktop/pic1.jpg", 1);
if (pic.empty())
{
std::cout << " Read failed " << std::endl;
return -1;
}
Point p1(900, 900);
putText(pic, "milkman", p1, FONT_HERSHEY_COMPLEX, 2, cv::Scalar(0, 0, 0), 3, 8, 0);
char time = system("time");
putText(pic, str_time, Point(1000, 700), FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0, 0, 0), 3, 8, 0);
rectangle(pic, Point(579, 290), Point(815, 622), cv::Scalar(255, 255, 255), -1);
imshow("test", pic);
waitKey(0);
char ch = waitKey(0);
if(ch == 38){
scale_up_x += 0.1;
resize(pic, scaled_f_up, Size(), scale_up_x, scale_up_y, INTER_LINEAR);
imshow("scaled_f_up", scaled_f_up);
waitKey(0);
}
if(ch == 40){
scale_up_y -= 0.1;
resize(pic, scaled_f_up, Size(), scale_up_x, scale_up_y, INTER_LINEAR);
imshow("scaled_f_up", scaled_f_up);
waitKey(0);
}
if(ch == 27){
break;
}
scanf("%d", &i);
}
return 0;
}
//python
#mport cv2
import numpy as np
image = cv2.imread('image.jpg')
cv2.imshow('Original Image', image)
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resized_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)
up_width = 600
up_height = 400
up_points = (up_width, up_height)
resized_up = cv2.resize(image, up_points, interpolation= cv2.INTER_LINEAR)
cv2.imshow('Resized Down by defining height and width', resized_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining height and width', resized_up)
cv2.waitKey()
//
//C++
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat image = imread("image.jpg");
imshow("Original Image", image);
int down_width = 300;
int down_height = 200;
Mat resized_down;
resize(image, resized_down, Size(down_width, down_height), INTER_LINEAR);
int up_width = 600;
int up_height = 400;
Mat resized_up;
resize(image, resized_up, Size(up_width, up_height), INTER_LINEAR)
imshow("Resized Down by defining height and width", resized_down);
waitKey();
imshow("Resized Up image by defining height and width", resized_up);
waitKey();
destroyAllWindows();
return 0;
}
//
//cv2.destroyAllWindows() or destroyAllWindows(); end //The learning
opencv The function is realized with C++ Than python fast , But it is more difficult than python Much larger . I think my code should not be the simplest and most efficient , So please correct any mistakes , There are also some ideas that may not change very well , The premise of writing good code is to be solid in the basic skills of programming language , I hope I can learn more and write more code , Let my code mature slowly , And beginner opencv Some knowledge is really difficult , You can check it out B Station video and so on .
边栏推荐
- Selenium basic knowledge automatic login QQ space
- 【sklearn】PCA
- Detailed explanation of VAO
- Selenium basics controls the scroll bar of the browser
- NFT是什么?一篇文章搞懂NFT的概念
- Advanced part of C language IV. detailed explanation of user-defined types
- OpenGL camera and periodic review
- Selenium basic knowledge automatically login Baidu online disk
- Introduction to C language III Array 4. Operators
- Li Kou, niuke.com - > linked list related topics (Article 1) (C language)
猜你喜欢

Game three piece chess

Hcip day 8 notes

Implement a queue with two stacks.

Stable TTL serial port rate supported by Arduino under different dominant frequencies

Selenium basic knowledge automatically login Baidu Post Bar

觉维设计响应式布局

Appium use

Introduction to C language III Array 4. Operators

Requests crawler implements a simple web page collector

Collection of linked list topics
随机推荐
13.Unity2D 横版 可上下左右移动的双向平台(双向行走+可移动+单独判定)+随机平台生成
【sklearn】PCA
[Huawei] Huawei machine test question-105
Starting from scratch C language intensive Part 3: Functions
Devops essay
Talk about compilers based on vscode
About how to set colored fonts on the terminal
Mitre att & CK ultra detailed learning notes-02 (a large number of cases)
Intelligent robots and intelligent systems (Professor Zheng Zheng of Dalian University of Technology) -- 2. Mobile Robot Perception
Selenium basics controls the scroll bar of the browser
Amber tutorial A17 learning - concept
Advanced part of C language I. data storage
MySQL -- subquery scalar subquery
mysql update 使用case when根据某一字段的值,更新另一字段的值
MySQL 啥时候用表锁,啥时候用行锁?
Function analysis of e-commerce website development and construction
Error when using PIP: pip is configured with locations that requires tls/ssl
JS_ Realize the separation of multiple lines of text into an array according to the newline
C language advanced part III. string functions and memory operation functions
Flinksql UDF custom data source
https://blog.csdn.net/Burp_Boom/article/details/124163830?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165823862616782246495908%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165823862616782246495908&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-9-124163830-null-null.142^v32^pc_search_v2,185^v2^control&utm_term=opencv%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8Bvs2022&spm=1018.2226.3001.4187