Ask Your Question

Revision history [back]

i was intending to develop a commercial software about OMR. now i hit my leg and share this code :)

i hope it will be helpful. ( i will add some explanation about the code later)

Test Image ( edited your image. having an empty and invalid double mark )

image description

Result Image

image description

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat img = imread(argv[1]);
    if(img.empty())
    {
        return -1;
    }

    Mat gray,thresh;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    threshold(gray, thresh, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);

    vector<Point2f> corners;
    vector<vector<Point> > contours;

    findContours(thresh.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    for( size_t i = 0; i< contours.size(); i++ )
    {
        RotatedRect minRect = minAreaRect( Mat(contours[i]) );

        // rotated rectangle
        Point2f rect_points[4];
        minRect.points( rect_points );
        if(minRect.size.height > img.cols / 2)
            for( int j = 0; j < 4; j++ )
            {
                corners.push_back(rect_points[(j+2)%4]);
            }
    }

    erode(thresh,thresh,Mat(),Point(-1,-1), 10);
    dilate(thresh,thresh,Mat(),Point(-1,-1), 5);

    Mat quad(Size(1000,250), CV_8UC1);
    Mat results(Size(1000,250), CV_8UC3);

    vector<Point2f> quad_pts;
    quad_pts.push_back(cv::Point2f(0, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));
    quad_pts.push_back(cv::Point2f(0, quad.rows));

    Mat transmtx = getPerspectiveTransform(corners, quad_pts);
    warpPerspective( img, results, transmtx, Size(1000,250)); // Create a Mat To Show results
    warpPerspective( thresh, quad, transmtx, Size(1000,250));

    resize(quad,quad,Size(20,5));

    for(int i = 0; i < quad.cols; i++)
    {
        String answer = "";

        answer += quad.at<uchar>(1,i) == 0 ? "" : "A";
        answer += quad.at<uchar>(2,i) == 0 ? "" : "B";
        answer += quad.at<uchar>(3,i) == 0 ? "" : "C";
        answer += quad.at<uchar>(4,i) == 0 ? "" : "D";

        if( answer.length()  > 1 ) answer = "X"; // Double mark

        putText( results, answer, Point( 50* i + 10, 40), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255),2);

    }
    imshow( "results", results );
    waitKey(0);

    return 0;
}

i was intending to develop a commercial software about OMR. now i hit my leg and share this code :)

i hope it will be helpful. ( i will add some explanation about the code later)

Test Image ( edited your image. having an empty and invalid double mark )

image description

Result Image

image descriptionimage description

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat img = imread(argv[1]);
    if(img.empty())
    {
        return -1;
    }

    Mat gray,thresh;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    threshold(gray, thresh, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);

    vector<Point2f> corners;
    vector<vector<Point> > contours;

    findContours(thresh.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    for( size_t i = 0; i< contours.size(); i++ )
    {
        RotatedRect minRect = minAreaRect( Mat(contours[i]) );

        // rotated rectangle
        Point2f rect_points[4];
        minRect.points( rect_points );
        if(minRect.size.height > img.cols / 2)
            for( int j = 0; j < 4; j++ )
            {
                corners.push_back(rect_points[(j+2)%4]);
            }
    }

    erode(thresh,thresh,Mat(),Point(-1,-1), 10);
    dilate(thresh,thresh,Mat(),Point(-1,-1), 5);

    Mat quad(Size(1000,250), CV_8UC1);
    Mat results(Size(1000,250), CV_8UC3);

    vector<Point2f> quad_pts;
    quad_pts.push_back(cv::Point2f(0, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));
    quad_pts.push_back(cv::Point2f(0, quad.rows));

    Mat transmtx = getPerspectiveTransform(corners, quad_pts);
    warpPerspective( img, results, transmtx, Size(1000,250)); // Create a Mat To Show results
    warpPerspective( thresh, quad, transmtx, Size(1000,250));

    resize(quad,quad,Size(20,5));

    for(int i = 0; i < quad.cols; i++)
    {
        String answer = "";

        answer += quad.at<uchar>(1,i) == 0 ? "" : "A";
        answer += quad.at<uchar>(2,i) == 0 ? "" : "B";
        answer += quad.at<uchar>(3,i) == 0 ? "" : "C";
        answer += quad.at<uchar>(4,i) == 0 ? "" : "D";

        if( answer.length()  > 1 ) answer = "X"; // Double mark
         int y = 0;
        if( answer == "A" ) y = 50;
        if( answer == "B" ) y = 100;
        if( answer == "C" ) y = 150;
        if( answer == "D" ) y = 200;
        if( answer == "" ) answer = "[-]";
        putText( results, answer, Point( 50* i + 10, 40), 15, 30 + y), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255),2);

    }
    imshow( "results", results );
    waitKey(0);

    return 0;
}

as a challenge to myself i tried to implement main part in JAVA ( a newcomer copy paste code )

Mat img = Imgcodecs.imread("test.jpg");
Mat gray = new Mat();
Mat thresh = new Mat();

//convert the image to black and white
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);

//convert the image to black and white does (8 bit)
Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
Mat temp = thresh.clone();
//find the contours
Mat hierarchy = new Mat();

Mat corners = new Mat(4,1,CvType.CV_32FC2);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
hierarchy.release();

for (int idx = 0; idx < contours.size(); idx++)
{
    MatOfPoint contour = contours.get(idx);
    MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
    RotatedRect minRect = Imgproc.minAreaRect( contour_points );
    Point[] rect_points = new Point[4];
    minRect.points( rect_points );
    if(minRect.size.height > img.width() / 2)
    {
        List<Point> srcPoints = new ArrayList<Point>(4);
        srcPoints.add(rect_points[2]);
        srcPoints.add(rect_points[3]);
        srcPoints.add(rect_points[0]);
        srcPoints.add(rect_points[1]);

        corners = Converters.vector_Point_to_Mat(
                      srcPoints, CvType.CV_32F);
    }

}
Imgproc.erode(thresh, thresh, new Mat(), new Point(-1,-1), 10);
Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1,-1), 5);
Mat results = new Mat(1000,250,CvType.CV_8UC3);
Mat quad = new Mat(1000,250,CvType.CV_8UC1);

List<Point> dstPoints = new ArrayList<Point>(4);
dstPoints.add(new Point(0, 0));
dstPoints.add(new Point(1000, 0));
dstPoints.add(new Point(1000, 250));
dstPoints.add(new Point(0, 250));
Mat quad_pts = Converters.vector_Point_to_Mat(
                   dstPoints, CvType.CV_32F);

Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);
Imgproc.warpPerspective( img, results, transmtx, new Size(1000,250));
Imgproc.warpPerspective( thresh, quad, transmtx, new Size(1000,250));

Imgproc.resize(quad,quad,new Size(20,5));

Imgcodecs.imwrite("results.png",quad);

here is the result image : image description

i was intending to develop a commercial software about OMR. now i hit my leg and share this code :)

i hope it will be helpful. ( i will add some explanation about the code later)

Test Image ( edited your image. having an empty and invalid double mark )

image description

Result Image

image description

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat img = imread(argv[1]);
    if(img.empty())
    {
        return -1;
    }

    Mat gray,thresh;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    threshold(gray, thresh, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);

    vector<Point2f> corners;
    vector<vector<Point> > contours;

    findContours(thresh.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    for( size_t i = 0; i< contours.size(); i++ )
    {
        RotatedRect minRect = minAreaRect( Mat(contours[i]) );

        // rotated rectangle
        Point2f rect_points[4];
        minRect.points( rect_points );
        if(minRect.size.height > img.cols / 2)
            for( int j = 0; j < 4; j++ )
            {
                corners.push_back(rect_points[(j+2)%4]);
            }
    }

    erode(thresh,thresh,Mat(),Point(-1,-1), 10);
    dilate(thresh,thresh,Mat(),Point(-1,-1), 5);

    Mat quad(Size(1000,250), CV_8UC1);
    Mat results(Size(1000,250), CV_8UC3);

    vector<Point2f> quad_pts;
    quad_pts.push_back(cv::Point2f(0, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));
    quad_pts.push_back(cv::Point2f(0, quad.rows));

    Mat transmtx = getPerspectiveTransform(corners, quad_pts);
    warpPerspective( img, results, transmtx, Size(1000,250)); // Create a Mat To Show results
    warpPerspective( thresh, quad, transmtx, Size(1000,250));

    resize(quad,quad,Size(20,5));

    for(int i = 0; i < quad.cols; i++)
    {
        String answer = "";

        answer += quad.at<uchar>(1,i) == 0 ? "" : "A";
        answer += quad.at<uchar>(2,i) == 0 ? "" : "B";
        answer += quad.at<uchar>(3,i) == 0 ? "" : "C";
        answer += quad.at<uchar>(4,i) == 0 ? "" : "D";

        if( answer.length()  > 1 ) answer = "X"; // Double mark
        int y = 0;
        if( answer == "A" ) y = 50;
        if( answer == "B" ) y = 100;
        if( answer == "C" ) y = 150;
        if( answer == "D" ) y = 200;
        if( answer == "" ) answer = "[-]";
        putText( results, answer, Point( 50* i + 15, 30 + y), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255),2);

    }
    imshow( "results", results );
    waitKey(0);

    return 0;
}

as a challenge to myself i tried to implement main part in JAVA ( a newcomer copy paste code )

here is the result image (20x5) : image description

Mat img = Imgcodecs.imread("test.jpg");
Mat gray = new Mat();
Mat thresh = new Mat();

//convert the image to black and white
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);

//convert the image to black and white does (8 bit)
Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
Mat temp = thresh.clone();
//find the contours
Mat hierarchy = new Mat();

Mat corners = new Mat(4,1,CvType.CV_32FC2);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
hierarchy.release();

for (int idx = 0; idx < contours.size(); idx++)
{
    MatOfPoint contour = contours.get(idx);
    MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
    RotatedRect minRect = Imgproc.minAreaRect( contour_points );
    Point[] rect_points = new Point[4];
    minRect.points( rect_points );
    if(minRect.size.height > img.width() / 2)
    {
        List<Point> srcPoints = new ArrayList<Point>(4);
        srcPoints.add(rect_points[2]);
        srcPoints.add(rect_points[3]);
        srcPoints.add(rect_points[0]);
        srcPoints.add(rect_points[1]);

        corners = Converters.vector_Point_to_Mat(
                      srcPoints, CvType.CV_32F);
    }

}
Imgproc.erode(thresh, thresh, new Mat(), new Point(-1,-1), 10);
Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1,-1), 5);
Mat results = new Mat(1000,250,CvType.CV_8UC3);
Mat quad = new Mat(1000,250,CvType.CV_8UC1);

List<Point> dstPoints = new ArrayList<Point>(4);
dstPoints.add(new Point(0, 0));
dstPoints.add(new Point(1000, 0));
dstPoints.add(new Point(1000, 250));
dstPoints.add(new Point(0, 250));
Mat quad_pts = Converters.vector_Point_to_Mat(
                   dstPoints, CvType.CV_32F);

Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);
Imgproc.warpPerspective( img, results, transmtx, new Size(1000,250));
Imgproc.warpPerspective( thresh, quad, transmtx, new Size(1000,250));

Imgproc.resize(quad,quad,new Size(20,5));

Imgcodecs.imwrite("results.png",quad);

here is the result image : image description

i was intending to develop a commercial software about OMR. now i hit my leg and share this code :)

i hope it will be helpful. ( i will add some explanation about the code later)

Test Image ( edited your image. having an empty and invalid double mark )

image description

Result Image

image description

EDIT

i updated the C++ code ( a small bug is fixed )

a variable Size dims added. by changing it you can find marks on different dimensions.

image description image description

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat img = imread(argv[1]);
    if(img.empty())
    {
        return -1;
    }

    Size dims(20,5); // this variable should be changed according input
    Mat gray,thresh;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    threshold(gray, thresh, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);

    Mat quad(img.size(), CV_8UC1); // should be improved
    Mat results(img.size(), CV_8UC3);

    vector<Point2f> quad_pts;
    quad_pts.push_back(cv::Point2f(0, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));
    quad_pts.push_back(cv::Point2f(0, quad.rows));

    vector<Point2f> corners;
    vector<vector<Point> > contours;

    findContours(thresh.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    for( size_t i = 0; i< contours.size(); i++ )
    {
        RotatedRect minRect = minAreaRect( Mat(contours[i]) );

        // rotated rectangle
        Point2f rect_points[4];
        minRect.points( rect_points );
        if(minRect.size.height 
        if(Rect(minRect.boundingRect()).width > img.cols / 2)
2) // should be improved
            for( int j = 0; j < 4; j++ )
            {
                corners.push_back(rect_points[(j+2)%4]);
Point2f pt = quad_pts[j];
              Point2f nearest_pt = rect_points[0];
              float dist = norm( pt - nearest_pt );
                for( int k = 1; k < 4; k++ )
                {
                 if( norm( pt - rect_points[k] ) < dist )
                 {
                   dist = norm( pt - rect_points[k] );
                   nearest_pt = rect_points[k];
                 }
     }
                corners.push_back( nearest_pt );
            }
    }

    erode(thresh,thresh,Mat(),Point(-1,-1), 10);
10); // should be improved
    dilate(thresh,thresh,Mat(),Point(-1,-1), 5);

    Mat quad(Size(1000,250), CV_8UC1);
    Mat results(Size(1000,250), CV_8UC3);

    vector<Point2f> quad_pts;
    quad_pts.push_back(cv::Point2f(0, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, 0));
    quad_pts.push_back(cv::Point2f(quad.cols, quad.rows));
    quad_pts.push_back(cv::Point2f(0, quad.rows));
5); // should be improved

    Mat transmtx = getPerspectiveTransform(corners, quad_pts);
    warpPerspective( img, results, transmtx, Size(1000,250)); img.size()); // Create a Mat To Show results
    warpPerspective( thresh, quad, transmtx, Size(1000,250));

    resize(quad,quad,Size(20,5));
img.size());

    resize(quad,quad,dims);

    for(int i = 0; i < quad.cols; i++)
    {
        String answer = "";

        answer += quad.at<uchar>(1,i) == 0 ? "" : "A";
        answer += quad.at<uchar>(2,i) == 0 ? "" : "B";
        answer += quad.at<uchar>(3,i) == 0 ? "" : "C";
        answer += quad.at<uchar>(4,i) == 0 ? "" : "D";

        if( answer.length()  > 1 ) answer = "X"; // Double mark
        int y = 0;
        if( answer == "A" ) y = 50;
results.rows / dims.height;
        if( answer == "B" ) y = 100;
results.rows / dims.height *2;
        if( answer == "C" ) y = 150;
results.rows / dims.height *3;
        if( answer == "D" ) y = 200;
results.rows / dims.height *4;
        if( answer == "" ) answer = "[-]";
        putText( results, answer, Point( 50* i + 15, 30 + y), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255),2);

    }
    imshow( "results", results );
    waitKey(0);

    return 0;
}

as a challenge to myself i tried to implement main part in JAVA ( a newcomer copy paste code )

here is the result image (20x5) : image description

Mat img = Imgcodecs.imread("test.jpg");
Mat gray = new Mat();
Mat thresh = new Mat();

//convert the image to black and white
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);

//convert the image to black and white does (8 bit)
Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
Mat temp = thresh.clone();
//find the contours
Mat hierarchy = new Mat();

Mat corners = new Mat(4,1,CvType.CV_32FC2);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
hierarchy.release();

for (int idx = 0; idx < contours.size(); idx++)
{
    MatOfPoint contour = contours.get(idx);
    MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
    RotatedRect minRect = Imgproc.minAreaRect( contour_points );
    Point[] rect_points = new Point[4];
    minRect.points( rect_points );
    if(minRect.size.height > img.width() / 2)
    {
        List<Point> srcPoints = new ArrayList<Point>(4);
        srcPoints.add(rect_points[2]);
        srcPoints.add(rect_points[3]);
        srcPoints.add(rect_points[0]);
        srcPoints.add(rect_points[1]);

        corners = Converters.vector_Point_to_Mat(
                      srcPoints, CvType.CV_32F);
    }

}
Imgproc.erode(thresh, thresh, new Mat(), new Point(-1,-1), 10);
Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1,-1), 5);
Mat results = new Mat(1000,250,CvType.CV_8UC3);
Mat quad = new Mat(1000,250,CvType.CV_8UC1);

List<Point> dstPoints = new ArrayList<Point>(4);
dstPoints.add(new Point(0, 0));
dstPoints.add(new Point(1000, 0));
dstPoints.add(new Point(1000, 250));
dstPoints.add(new Point(0, 250));
Mat quad_pts = Converters.vector_Point_to_Mat(
                   dstPoints, CvType.CV_32F);

Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);
Imgproc.warpPerspective( img, results, transmtx, new Size(1000,250));
Imgproc.warpPerspective( thresh, quad, transmtx, new Size(1000,250));

Imgproc.resize(quad,quad,new Size(20,5));

Imgcodecs.imwrite("results.png",quad);