Ask Your Question

Find projections of an image and divide it vertically and horizontally

asked 2018-08-01 05:37:37 -0500

hrishi22 gravatar image

I want to get vertical, horizontal projections and check if there are 2 or more zero values in the projection. I am working on a shelf image, where I want to detect all the shelves and for each shelf, segment the products vertically. I am thinking of the following approach.C:\fakepath\flow.png. I have attached the desired ouput image also.C:\fakepath\project.png. If I get that, I was thinking of getting a grid on the image so that i can get the coordinates for the line segments achieved vertically and horizontally.


Shelf Image - Horizontal shelf detection - vertical product segmentation - getting coordinates for the intersetions(output)

I am working with opencv. Appreciate assistance!

Thanks, Hrishi

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-08-03 18:22:54 -0500

have a look at this (from

void projection2(Mat src,vector<int>& vup,vector<int>& vdown,int direction,int gap){
        Mat tmp = src.clone();
        vector<int> vdate;
        if (DIRECTION_X == direction){
            for (int i=0;i<tmp.cols;i++){
                Mat data = tmp.col(i);
                int itmp = countNonZero(data);
            for (int i=0;i<tmp.rows;i++){
                Mat data = tmp.row(i);
                int itmp = countNonZero(data);
        //avoid gap
        if (vdate.size()<=gap)
        for (int i=0;i<vdate.size()-gap;i++){
            if (vdate[i]>0 && vdate[i+gap]>0){
                for (int j=i;j<i+gap;j++){
                    vdate[j] = 1;
                i = i+gap-1;
        //up and down
        for (int i=1;i<vdate.size();i++){
            if (vdate[i-1] == 0 && vdate[i]>0)
            if (vdate[i-1]>0 && vdate[i] == 0)

and i do this to get the vector<int>

 dstclone = ostu.clone();
 // get the vector<int>
 for (int i=0;i<ostu.cols;i++)
        Mat data = ostu.col(i);
        int itmp = countNonZero(data);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-08-01 05:37:37 -0500

Seen: 123 times

Last updated: Aug 03 '18