4

I have to find the ruler position from an image by using opencv.I am able to detect the color of the ruler(green). How can i read all the pixel from an image and get the upper and lower position of ruler.

void findrulerPosition(cv::Mat image, int indx) {
std::stringstream ss;//create a stringstream
ss << indx;//add number to the stream

cv::Mat hsv;
cvtColor(image, hsv, CV_BGR2HSV);

String filename = OUTPUT_FOLDER + "hsv" + ss.str() + ".png";
imwrite(filename, hsv );

cv::Mat hsvbw;
inRange(hsv, cv::Scalar(30,0,0), cv::Scalar(80, 255, 255), hsvbw);
//inRange(hsv, cv::Scalar(12,255,255), cv::Scalar(23, 245, 255), hsvbw);
   //inRange(image, cv::Scalar(0,64,255), cv::Scalar(0, 207, 255), hsvbw);

filename = OUTPUT_FOLDER + "hsvbw" + ss.str() + ".png";
imwrite(filename, hsvbw );


vector<vector<Point> > contours;
findContours(hsvbw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
cv::Mat dst = Mat::zeros(image.size(), image.type());
drawContours(dst, contours, -1, Scalar::all(255), CV_FILLED);

this->cmpddst &=  dst;
dst &= image;
this->cmpddst &=  image;

filename = OUTPUT_FOLDER + "cmpddst" + ss.str() + ".png";
imwrite(filename, this->cmpddst );


filename = OUTPUT_FOLDER + "dst" + ss.str() + ".png";
imwrite(filename, dst );

}

kashif
  • 153
  • 10
  • i am new here so not allowed to upload image – kashif Dec 10 '12 at 22:26
  • 1
    @kashifhussain : upload image in imageshack.us or any other image sharing sites (google for it) and provide the link here. – Abid Rahman K Dec 11 '12 at 00:57
  • @AbidRahmanK . thanks. links is http://imageshack.us/photo/my-images/571/dst27.png/ how i will get the upper and lower position of the ruler? 2 rulere are in the image – kashif Dec 11 '12 at 17:21
  • @kashifhussain please, take a look at my answer and see if it helps you. – ArtemStorozhuk Dec 13 '12 at 17:37
  • @Astor Thanks for help. I tried you code its working well . I have 2 question relating code. where you are matching the low and up position for ruler. when i run code the the result for low and up is Low( 6 ,478) and up(4 ,464) and how i will do the same thing (low and up) for the second ruler because in output image the pink color it shows only for one ruler. once again thanks for help – kashif Dec 15 '12 at 01:07

1 Answers1

3

Here's what I've done:

  1. A bit improved your green range because yours is not detecting green color - it's detecting many other colors.
  2. Find contours on image.
  3. Find contour with area bigger than 100.
  4. Find up and low points of contour.
  5. Draw these 2 points.
Mat src = imread("input.png"), tmp;
cvtColor(src, tmp, CV_BGR2HSV_FULL);
inRange(tmp, Scalar(50, 50, 50), Scalar(70, 255, 255), tmp);

vector<vector<Point> > contours;
findContours(tmp, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

int upY = INT_MAX, lowY = 0, upX, lowX;
for (int i=0; i<contours.size(); i++)
{
    if (contourArea(contours[i]) > 100)
    {
        for (int j=0; j<contours[i].size(); j++)
        {
            if (contours[i][j].y > lowY)
            {
                lowY = contours[i][j].y;
                lowX = contours[i][j].x;
            }
            if (contours[i][j].y < upY)
            {
                upY = contours[i][j].y;
                upX = contours[i][j].x;
            }
        }

        cout << "low = (" << lowX << ", " << lowY << ")"<<  endl
             << "up  = (" << upX << ", " << upY << ")"<<  endl;
        break;
    }
}

circle(src, Point(lowX, lowY), 3, Scalar(255, 0, 255));
circle(src, Point(upX, upY), 3, Scalar(255, 0, 255));

imshow("Window", src);
waitKey();

Here's result:

enter image description here

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53