Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Cách thay đổi kích thước ảnh và thêm viền trong OpenCV C++


Giới thiệu

Ứng dụng trackbar đa chức năng: thay đổi kích thước (scale), xoay (angle), thêm viền (border mode) cùng lúc trên một ảnh.

Chương trình đa trackbar

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

int Rotate = 180, Scale = 50, Border = 0;
Mat before_Rotate;
int vertical = 0, horizontal = 0;

void rotator(int, void*) {
    Mat Rotation = getRotationMatrix2D(Point(horizontal, vertical), Rotate - 180, Scale / 50.0);
    Mat Rotated;
    warpAffine(before_Rotate, Rotated, Rotation, before_Rotate.size(), INTER_LINEAR, Border, Scalar());
    imshow("RotatedImage", Rotated);
}

int main(int argc, char** argv) {
    before_Rotate = imread("sky.jpg");
    vertical = before_Rotate.rows / 2;
    horizontal = before_Rotate.cols / 2;
    namedWindow("BeforeRotate"); imshow("BeforeRotate", before_Rotate);
    namedWindow("AfterRotate");
    createTrackbar("Angle", "AfterRotate", &Rotate, 360, rotator);
    createTrackbar("Scale", "AfterRotate", &Scale, 100, rotator);
    createTrackbar("Border Mode", "AfterRotate", &Border, 5, rotator);
    rotator(0, 0);
    waitKey(0);
    return 0;
}

Kết quả

Cửa sổ "BeforeRotate" ảnh gốc. "AfterRotate" ảnh đã biến đổi. 3 trackbar: Angle (0-360), Scale (0-100% → 0-2.0), Border Mode (0-5 các chế độ viền OpenCV).