How to extract color patches from image?

asked 2018-01-14 06:18:20 -0600

Vishudh gravatar image

updated 2018-01-14 06:19:52 -0600

Hello I am knew to OpenCV. I have just started developing with Android and OpenCV. I have an image as given below. I need to extract those ten square color patches from it. What method(s)/procedure should I follow? I tried the following code snppet I got from a book. But it is not giving me the desired results.

Mat mRgba = new Mat();;

    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat heirarchy = new Mat();

    Bitmap actualImage32 = actualImage.copy(Bitmap.Config.ARGB_8888, true);
    Utils.bitmapToMat(actualImage32, mRgba);

    List<MatOfPoint> contourList = new ArrayList<MatOfPoint>();//List of all contours

    //image to grayscale
    Imgproc.cvtColor(mRgba,grayMat,Imgproc.COLOR_BGR2GRAY);

    Imgproc.Canny(grayMat,cannyEdges,10,100);

    //find contours
    Imgproc.findContours(cannyEdges,contourList,heirarchy,Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);

    //Drawing contours on new Image
    Mat contours = new Mat();
    contours.create(cannyEdges.rows(),cannyEdges.cols(), CvType.CV_8UC3);
    Random r = new Random();

    for(int i = 0;i<contourList.size();i++){
        Imgproc.drawContours(contours,contourList,i,new Scalar(r.nextInt(255),r.nextInt(255),r.nextInt(255),-1));
    }
    //Converting Mat back back to Bitmap
    //Utils.matToBitmap(contours,***************);
    //loadImagetoImageView();
    Utils.matToBitmap(contours, actualImage);
    ImageView imgView = (ImageView) findViewById(R.id.strip_image);
    imgView.setImageBitmap(actualImage);

image description. x

edit retag flag offensive close merge delete

Comments

1

Reminds me of swimming.

Do you understand C++? Does Android not support C++?

sjhalayka gravatar imagesjhalayka ( 2018-01-15 21:06:41 -0600 )edit

Yes, I understand C++. I have worked on some C++ projects. I had posted this same question here From the answer someone has suggested, I am thresholding the S channel and finding contours from it. But I am failing to identity the first first 3 patches, which are of almost same color. Could you advice me any other method/steps for finding contours and then identify the patches?

Vishudh gravatar imageVishudh ( 2018-01-17 00:26:44 -0600 )edit

I took a look at it and didn't really find a general solution to your problem. Perhaps you need to be able to make some guarantees, like the strip will be in the same position and orientation all of the time. That way you can pre-define the 10 regions of interest. You have to think like the computer.

sjhalayka gravatar imagesjhalayka ( 2018-01-17 12:04:25 -0600 )edit

You are right. It seems like I have to add some pattern as background.

Vishudh gravatar imageVishudh ( 2018-01-18 02:22:25 -0600 )edit