opencv识别魔方

cube.cpp
#include "cube.hpp"

int check(Mat imgThresholded)
{
    int false1 = 0;
    for (int y = 0; y < imgThresholded.rows; y++)
    {
        for (int x = 0; x < imgThresholded.cols; x++)
        {
            if (imgThresholded.at<uchar>(y, x) == 255)
            {
                false1 = 1; break;
            }
            if (false1 == 1)
                break;
        }
    }
    return false1;
}

//2.提取蓝色
int cube_detector::check_color(Mat src)
{
    Mat imgHSV;
    vector<Mat> hsvSplit;
    cvtColor(src, imgHSV, COLOR_BGR2HSV);
    Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
    Mat imgThresholded1, imgThresholded2, imgThresholded3, imgThresholded4, imgThresholded5, imgThresholded6;
    inRange(imgHSV, Scalar(90, 43, 46), Scalar(125, 255, 255), imgThresholded1);                                  /*判断九个块是否有 **蓝色**,如果有,令a=1           */
    morphologyEx(imgThresholded1, imgThresholded1, MORPH_OPEN, element);
    imshow("提取蓝色", imgThresholded1);
    int a = check(imgThresholded1);
    if (a == 1)
        cout << "not complete";
    else
        cout << "complete";
    waitKey(0);
    return a;
}



//1.去背景
Mat cube_detector::getMat(Mat img)
{
    Mat img1, img2;                      //img2为结果图,img1为原图,img是接下来要经过二值化处理的图      
    //读入原图
    img.copyTo(img1);
    cvtColor(img, img, COLOR_BGR2HSV);
    GaussianBlur(img, img, Size(5, 5), 0, 0);
    cvtColor(img, img, COLOR_BGR2GRAY);                               //对img进行一系列处理二值化
    threshold(img, img, 140, 255, THRESH_BINARY);
    imshow("二值化", img);
    medianBlur(img, img, 3);
    img1.copyTo(img2, img);     //核心掩膜处理
    imshow("背景去除图", img2);
    return img2;
}

/*
    1.白色块和红色块容易丢失,不能大量出现在边上,狗则可能会丢失蓝色快导致出错
    2.棕色木板光照强时不易去除干净

    **可修改**:
                去背景阈值:木板背景去除问题
*/

    
#include "cube.hpp"



int main()
{
    cube_detector cube;
    Mat frame, dstImage1,result1,result2;
    int key=0;
    
    /*VideoCapture cap;
    // open the default camera using default API
    cap.open(0);
    // OR advance usage: select any API backend
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    // open selected camera using selected API
    cap.open(deviceID + apiID);
    if (!cap.isOpened()) {
        std::cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    else
    {
        cap >> frame;*/
        frame = imread("E://picture/WIN_20180806_17_41_43_Pro.jpg");

        if (frame.empty()) {
            std::cerr << "ERROR! blank frame grabbed\n";
        }
        resize(frame, dstImage1, Size(frame.cols / 3, frame.rows / 3), 0, 0, INTER_LINEAR);
        imshow("原图", dstImage1);
        result1 = cube.getMat(dstImage1);
        key = cube.check_color(result1);
    return key;
}
main.cpp
#pragma once

#include <vector>
#include <iostream>
#include <opencv2/opencv.hpp>



using namespace std;
using namespace cv;


class cube_detector
{
public:
    Mat cube_Find(Mat img);
    Mat getMat(Mat frame);
    int check_color(Mat src);
};
View Code

 

posted @ 2018-08-07 15:00  做条快乐的咸鱼  阅读(1810)  评论(1编辑  收藏  举报