This content originally appeared on DEV Community and was authored by victor_dalet
To install opencv in python
pip install opencv-python
To install opencv in c++
git clone https://github.com/opencv/opencv.git
mkdir -p build && cd build
cmake ../opencv
make -j4
sudo make install
CmakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(opencv_c__)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 17)
add_executable(opencv_c__ main.cpp)
target_link_libraries(opencv_c__ ${OpenCV_LIBS})
I - Mouvement detection
def ex1():
cap = cv2.VideoCapture(0)
object_detector = cv2.createBackgroundSubtractorMOG2()
while True:
ret, frame = cap.read()
mask = object_detector.apply(frame)
cv2.imshow('Video', mask)
if cv2.waitKey(30) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
int main() {
cv::VideoCapture cap(0);
cv::Ptr<cv::BackgroundSubtractor> object_detector = cv::createBackgroundSubtractorMOG2();
while (true) {
cv::Mat frame;
cap >> frame;
cv::Mat mask;
object_detector->apply(frame, mask);
cv::imshow("Video", mask);
if (cv::waitKey(30) == 27) {
break;
}
}
cap.release();
cv::destroyAllWindows();
return 0;
}
II - Blur faces
def ex2():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
roi = frame[y:y + h, x:x + w]
roi = cv2.GaussianBlur(roi, (23, 23), 30)
frame[y:y + h, x:x + w] = roi
cv2.imshow("gray", gray)
if cv2.waitKey(30) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
int main() {
cv::VideoCapture cap(0);
while(true) {
cv::Mat frame;
cap >> frame;
cv::CascadeClassifier face_cascade;
face_cascade.load("haarcascade_frontalface_default.xml");
std::vector<cv::Rect> faces;
face_cascade.detectMultiScale(frame, faces, 1.1, 3, 0, cv::Size(30, 30));
for(int i = 0; i < faces.size(); i++) {
cv::Rect face = faces[i];
cv::Mat faceROI = frame(face);
cv::blur(faceROI, faceROI, cv::Size(30, 30));
}
cv::imshow("frame", frame);
if(cv::waitKey(1) == 27) {
break;
}
}
}
III - Tracing a movement
def ex3():
cap = cv2.VideoCapture(0)
object_detector = cv2.createBackgroundSubtractorMOG2()
last_coordinates = []
while True:
ret, frame = cap.read()
mask = object_detector.apply(frame)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.contourArea(contour) < 5000:
continue
(x, y, w, h) = cv2.boundingRect(contour)
last_coordinates.append((x, y, w, h))
for i in range(1, len(last_coordinates)):
cv2.line(frame, (last_coordinates[i - 1][0], last_coordinates[i - 1][1]),
(last_coordinates[i][0], last_coordinates[i][1]), (0, 0, 255), 5)
cv2.imshow('Video', frame)
if cv2.waitKey(30) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
int main() {
cv::VideoCapture cap(0);
cv::Ptr<cv::BackgroundSubtractor> object_detector = cv::createBackgroundSubtractorMOG2();
std::vector<cv::Rect> last_coordinates;
while (true) {
cv::Mat frame;
cap >> frame;
cv::Mat mask;
object_detector->apply(frame, mask);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(mask, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
for (auto &contour : contours) {
if (cv::contourArea(contour) < 5000) {
continue;
}
cv::Rect rect = cv::boundingRect(contour);
last_coordinates.push_back(rect);
}
for (int i = 1; i < last_coordinates.size(); i++) {
cv::line(frame, cv::Point(last_coordinates[i - 1].x, last_coordinates[i - 1].y),
cv::Point(last_coordinates[i].x, last_coordinates[i].y), cv::Scalar(0, 0, 255), 5);
}
cv::imshow("Video", frame);
if (cv::waitKey(30) & 0xFF == 27) {
break;
}
}
cap.release();
cv::destroyAllWindows();
return 0;
}
This content originally appeared on DEV Community and was authored by victor_dalet
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
victor_dalet | Sciencx (2024-09-08T18:17:52+00:00) 3 example of computer vison technical test – Python / c++. Retrieved from https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/
" » 3 example of computer vison technical test – Python / c++." victor_dalet | Sciencx - Sunday September 8, 2024, https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/
HARVARDvictor_dalet | Sciencx Sunday September 8, 2024 » 3 example of computer vison technical test – Python / c++., viewed ,<https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/>
VANCOUVERvictor_dalet | Sciencx - » 3 example of computer vison technical test – Python / c++. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/
CHICAGO" » 3 example of computer vison technical test – Python / c++." victor_dalet | Sciencx - Accessed . https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/
IEEE" » 3 example of computer vison technical test – Python / c++." victor_dalet | Sciencx [Online]. Available: https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/. [Accessed: ]
rf:citation » 3 example of computer vison technical test – Python / c++ | victor_dalet | Sciencx | https://www.scien.cx/2024/09/08/3-example-of-computer-vison-technical-test-python-c/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.