OpenCV 第六天
OpenCV 第六天

OpenCV 第六天

Program a simple sketch utilizing OpenCV

#include<opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;
int MouseDown2=0;
void onMouseMove2(int event, int x, int y, int flags, void* src){
    Mat *img = (Mat*)src;
    if(x<=0 || y<=0 || x>=img->cols || y>=img->rows)
        return;
    if(event==EVENT_LBUTTONDOWN)
        MouseDown2 = 1;
    if(event == EVENT_LBUTTONUP)
        MouseDown2 = 0;

    if((event == EVENT_MOUSEMOVE && MouseDown2) || MouseDown2){
        circle(*img,Point2d(x,y),5,Scalar(255,255,255),5,8,0);
    }
}

int main(){
    Mat img = Mat::zeros(500,500,CV_8UC3);
    while(1){
        imshow("Sketch",img);
        setMouseCallback("Sketch",onMouseMove2,&img);
        if(waitKey(1)==27)
            break;
    }
    return 0;
}
Victory worth $200b.

Add a Customizable Color Panel

#include<opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;
int MouseDown2=0;
int SliderR = 255;
int SliderG = 255;
int SliderB = 255;
void onMouseMove2(int event, int x, int y, int flags, void* src){
    Mat *img = (Mat*)src;
    if(x<=0 || y<=0 || x>=img->cols || y>=img->rows)
        return;
    if(event==EVENT_LBUTTONDOWN)
        MouseDown2 = 1;
    if(event == EVENT_LBUTTONUP)
        MouseDown2 = 0;

    if((event == EVENT_MOUSEMOVE && MouseDown2) || MouseDown2){
        circle(*img,Point2d(x,y),3,Scalar(SliderB,SliderG,SliderR),8,8,0);
    }
}

int main(){
    namedWindow("Sketch",WINDOW_AUTOSIZE);
    Mat img = Mat::zeros(500,500,CV_8UC3);
    createTrackbar("R","Sketch",&SliderR,255);
    createTrackbar("G","Sketch",&SliderG,255);
    createTrackbar("B","Sketch",&SliderB,255);
    while(1){
        imshow("Sketch",img);
        setMouseCallback("Sketch",onMouseMove2,&img);
        if(waitKey(1)==27)
            break;
    }
    return 0;
}

New recipe —— FaceNet + OpenCV

from facenet_pytorch import MTCNN
import torch
import numpy as np
import mmcv, cv2
from PIL import Image, ImageDraw
from IPython import display

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Running on device: {}'.format(device))

cap = cv2.VideoCapture(cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1080)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
mtcnn = MTCNN(keep_all=True, device=device)

while cap.isOpened():
    ret,image = cap.read()
    frame = [Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))]
    boxes, _ = mtcnn.detect(frame[0])
    if boxes is None:
        boxes = 0
    if type(boxes) is not int:
        frame_draw = frame[0].copy()
        draw = ImageDraw.Draw(frame_draw)
        for box in boxes:
            draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)
        frame_draw.resize((640, 360), Image.BILINEAR)
        boxed_array = np.asarray(frame_draw)
        boxed_image = cv2.cvtColor(boxed_array,cv2.COLOR_RGB2BGR)
    else:
        boxed_image = image

    cv2.imshow("Face Recognition By BK",boxed_image)
    c = cv2.waitKey(32)
    if c == 27:
        break

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据