Small set of opencv tools

#include "stdafx.h"
#include "ImgFilters.h"
#include<opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

ImgFilters::ImgFilters()
{
}


ImgFilters::~ImgFilters()
{
}

//图片倒转
void ImgFilters::filp() {
    const char* filename = "G:\\b.png";
    Mat src = imread(filename);
    int flipCode = atoi(filename);
    if (src.empty()) {
        throw("Faild open file");
    }

    Mat dst;
    flip(src, dst, flipCode);

    imshow("src", src);
    //Display the original image
    imshow("dst", dst);
    //Display the flipped image

    waitKey();
}

//Image reversal
void ImgFilters::myResize() {
    const char *filename = "G:\\b.png";
    Mat src, dst;
    //Define the size of the image, reduce the width by 80%
    float scaleW = 0.8;
    // Define the size of the image, the height is reduced by 80%
    float scaleH = scaleW;

    //Decolorize the read image
    src = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
    if (src.empty()) {
        throw("Faild open file.");
    }
    int width = static_cast<float>(src.cols*scaleW) ;
    //Define the width you want to expand or reduce, src.cols is the width of the original image, multiply it by 80% to get the desired size, and cast it to float type
    int height = static_cast<float>(src.rows *scaleH);
    //Define the height you want to expand or reduce, src.cols is the height of the original image, multiply it by 80% to get the desired size, and force it to float

    resize(src, dst, Size(width, height));
    //function to redefine the size

    imshow("src", src);
    imshow("dst", dst);

    waitKey();

}

void ImgFilters::myRotate() {
    Mat src, dst;
    float angle = 55.5;
    //Define the flip angle as 55.5 degrees
    const char* filename = "G:\\b.png";
    src = imread(filename, CV_LOAD_IMAGE_GRAYSCALE) ;
    if (src.empty()) {
        throw("Faild open file.");
    }

    cv::Point2f center = cv::Point2f(static_cast<float>(src.cols / 2),
        static_cast<float>(src.rows / 2));
    //Define the coordinates of the center point, 2f means float , the center point is X, and Y is half of the width and height
    cv::Mat affineTrans = getRotationMatrix2D(center, angle, 1.0);
    //getRotationMatrix2D is the function of image rotation, the first parameter center is the rotation center point, The second parameter angle is the rotation angle
    //The third parameter of 1.0 is double scale: image scaling factor

    cv::warpAffine(src, dst, affineTrans, src.size(), cv::INTER_CUBIC, cv::BORDER_REPLICATE);
    //Affine transformation function warpAffine
    //The first parameter src is the input original image
    //The second The first parameter dst is the output image
    //The third parameter is the affineTrans transformation matrix
    //The fourth parameter is the size of the output image
    //The fifth parameter is the cubic variance method
    //The sixth parameter is the upper and lower lines for the boundary Or left and right columns to copy padding

    cv::imshow("src", src);
    cv::imshow("dst", dst);

    cv::imwrite("G:\\RotateDst.jpg", dst);
    //output image

    cv::waitKey();
}

void ImgFilters::circularity()
{
    Mat imageSource, image;


    imageSource = imread("G:\\b.png");


    cvtColor(imageSource, image, CV_BGR2GRAY);
    //Gaussian filter
    GaussianBlur(image, image, Size(9, 9), 2, 2);

    vector<Vec3f> circles;
    //霍夫圆
    HoughCircles(image, circles, CV_HOUGH_GRADIENT, 1.5, image.rows / 8, 200, 100, 0, 0);
    cout << "circles.size():"<<circles.size() << endl;
    for (size_t i = 0; i < circles.size(); i++)
    {

        cout << "圆心" <<i<<":"<< circles[i][0]<<","<<cvRound(circles[i][1]) << endl;
        cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
        int radius = cvRound(circles[i][2]);
        //绘制圆心  
        cv::circle(imageSource, center, 3, Scalar(0, 255, 0), -1, 8, 0);
        //绘制圆轮廓  
        cv::circle(imageSource, center, radius, Scalar(155, 50, 255), 3, 8, 0);
    }

 

    imshow("Point of Contours", imageSource); //All contour point sets saved in vector contours  


    waitKey();
}

void ImgFilters::imageCrop()
{


    Mat image = imread("G:\\b.png");
    if (image.empty()) {
        return;
    }
    Rect rect1(0, 0, 400, 400);


    Mat roi1;
    image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
    imshow("裁剪窗口", roi1);
    waitKey(0);

}

 

////////////////////////Small example of answer card identification /////////////////// /////////

 

class RectComp //Rect排序  
{
    Rect rm;
public:
    RectComp(Rect rms)
    {
        rm = rms;
    }
    bool operator< (const RectComp& ti) const
    {
        return rm.x < ti.rm.x;
    }
    Rect getRect() {
        return rm;
    }

};

void ImgFilters::exmple() {
    //Load image  
    Mat sourceImg = imread("G:\\aaa.png");
    Mat grayImg;

    //The picture becomes a grayscale picture  
    cvtColor(sourceImg, grayImg, CV_BGR2GRAY);

    Mat thresholdImg;
    //Image binarization  
    threshold(grayImg, thresholdImg, 200, 255, THRESH_BINARY_INV);


    Mat erodeImg;
    //Determine the size of the erosion and expansion core  
    Mat element = getStructuringElement(MORPH_RECT, Size(4, 4));
    //Corrosion operation  
    erode(thresholdImg, erodeImg, element);
    //Expansion operation  
    Mat dilateImg;
    dilate(erodeImg , dilateImg, element);
    //Determine the ROI area of ​​each answer sheet  
    Mat imag_ch1;
    dilateImg(Rect(5, 30, 95, 60)).copyTo(imag_ch1);

    imshow("img1", imag_ch1);

    //提取已经涂好了的选项  
    std::vector<std::vector<cv::Point> > chapter1;
    findContours(imag_ch1, chapter1, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    Mat result(imag_ch1.size(), CV_8U, cv::Scalar(255));
    cv::drawContours(result, chapter1, -1, cv::Scalar(0), 2);
    namedWindow("resultImage", 1);
    cv::imshow("resultImage", result);

    vector<RectComp>RectCompList;
    for (int i = 0; i<chapter1.size(); i++)
    {
        Rect rm = cv::boundingRect(cv::Mat(chapter1[i]));
        cout << "长方形" << i << ":" << "面积:" << rm.area() << endl;

        RectComp *ti = new RectComp(rm);
        RectCompList.push_back(*ti);
        // printf("Rect %dx = %d,y = %d \n",i,rm.x,rm.y);  
    }
    sort(RectCompList.begin(), RectCompList.end());
    map<int, string>listenAnswer;
    //Determine whether this part of the answer sheet has been painted  
    for (int t = 0; t<RectCompList.size() ; t++)
    {
        if (RectCompList.at(t).getRect().y<32)
        {
            listenAnswer[t] = "A";
        }
        else if ((RectCompList.at(t).getRect().y>32 ) && (RectCompList.at(t).getRect().y<48))
        {
            listenAnswer[t] = "B";
        }
        else if (RectCompList.at(t).getRect().y>48)
        {
            listenAnswer[t] = "C";
        }
        printf("sorted %d x = %d,y = %d \n", t, RectCompList.at(t).getRect().x, RectCompList.at(t).getRect().y);
    }

    for (map<int, string>::iterator it = listenAnswer.begin(); it != listenAnswer.end(); ++it)
    {
        cout << "num:" << it->first + 1 << "," << "answer:" << it->second << endl;
    }

    waitKey();
    
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208420&siteId=291194637