Ask Your Question

Tom James's profile - activity

2017-07-23 07:45:05 -0600 asked a question why did nvcc report errors in imgproc.hpp?

I wrote a small application which can get information from the camera and process the images with Open Cv functions. There are three files in my project: camera_detect.cu, detecting_functions.cu and DtctFun.hpp. Here are the codes of my files: camera_detect.cu:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
#include<core.hpp>
#include<highgui.hpp>
#include"DtctFun.hpp"
#include<utility.hpp>
#include<imgproc.hpp>
#include "math.h"
using namespace cv;
int main(int arg, char*argv[])
{
    //camera
    VideoCapture Cap;
    Cap.open(0); 
    Mat img;
    Mat objects;
    Mat labels,stats,centroids,colors;
    for(;;)
    {
            //put every point from the camera to an image
            Cap>>img;
            //img.convertTo(img,CV_8SC1);
            cvtColor(img,img,CV_RGB2GRAY);
            imshow("Camera",img);
            //img.convertTo(img,CV_8UC1);
            Mat pattern=calculateLightPattern(img);
            imshow("Background",pattern);
            medianBlur(img,img,7);
            medianBlur(pattern,pattern,7);
            //acquiring the img of objects only
            objects=removeLight(img,pattern,2);//using subtraction
            objects= leaveout(objects);
            objects=binarying(objects);
            if(!strcmp(argv[1],"dark"))//of the background is dark, reverse the images
                    objects=Reversing(objects);
            imshow("objects",objects);

            //connected commponents
            int number=connectedComponentsWithStats(objects,labels,stats,centroids);
            //labels=nosmallarea(labels,stats,number,10000);
            colors=colorlabels(labels,number);
            //mark the objects with words
            stringstream ss;
            int i;
            cout<<"total number of objects:"<<(int)number<<endl;
            for(i=0;i<number;i++)//write the number of the object on the picture
            {
                    cout<<"centroids of object"<<(int)i<<":"<<(Point2d)centroids.at<Point2d>(i)<<endl;
                    cout<<"object:"<<(int)i<<" area: "<<stats.at<int>(i,CC_STAT_AREA)<<" centroids: "<<(Point2d)centroids.at<Point2d>(i)<<endl;
ss.str("");
                    ss<<"object:"<<(int)i;//" area: "<<stats.at<int>(i,CC_STAT_AREA)<<" centroids: "<<(Point2d)centroids.at<Point2d>(i);
                    cout<<ss.str()<<endl;
                    putText(colors,ss.str(),centroids.at<Point2d>(i),FONT_HERSHEY_SIMPLEX,0.4,Scalar(255,255,255));
             }

            //output the result
            imshow("colored labels",colors);
            int a=waitKey(30);
            if(a!=255)
                    break;
    }
}

detecting_functions.cu:

#include "book.h"
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
//opencv 
#include<cv.hpp>
#include<core.hpp>
#include<highgui.hpp>
#include<utility.hpp>
#include"imgproc.hpp"
#include "math.h"
using namespace cv;

//Background
Mat calculateLightPattern(Mat img)
{
  Mat pattern;
  //Basic and effective way to calculate the light pattern form one image
  blur(img, pattern, Size(img.cols,img.cols));
  return pattern;
}


//generating random color
static Scalar randomColor( RNG& rng )
{
  int icolor = (unsigned) rng;
  return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
}


//making colorful labels
Mat colorlabels(Mat labels,int number)//convert labels in different colors, number is the amount of the objects
{
    Mat color=Mat::ones(labels.rows,labels.cols,CV_8UC3);;
    RNG rng(0xFFFFFFFF);

    int i=1;
    for(i=1;i<number;i++)
    {
            Mat mask= labels==i;
            color.setTo(randomColor(rng), mask);
    }

    return color;

}
//ignore the regions whose intensity is low(single chennal)
Mat leaveout(Mat img)
{
  int i,j;
  for(i=0;i<img.cols-1;i++)
    {
      for(j=0;j<img.rows-1;j++)
        {
          if(img.at<unsigned char>(j,i)<50)//at(rows,cols)
            {
              img.at<unsigned char>(j,i)=0;
            }
        }
    }
  return img;
}


//reversing the picture
Mat ...
(more)
2017-04-30 02:28:19 -0600 received badge  Enthusiast
2017-04-29 03:24:44 -0600 commented question OpenCV Error: Sizes of input arguments do not match when resize

I changed the "resize(pattern,pattern,Size((img.cols)/10,(img.rows)/10),CV_INTER_LINEAR);" into: "resize(pattern,pattern,Size((pattern.cols)/10,(pattern.rows)/10),CV_INTER_LINEAR);" And the problem seemed to be solved. However, the sizes of the two pictures should be the same and img.cols should be equal to pattern.cols. Why didn't the former code works?

2017-04-29 03:12:46 -0600 commented question OpenCV Error: Sizes of input arguments do not match when resize

the two pictures have been shown. But in my computer, the size of the two pictures are both 3120*4160, the size of the two images should be the same

2017-04-28 08:50:01 -0600 commented question OpenCV Error: Sizes of input arguments do not match when resize

the first argument of the main function which is argv[1] is used to indicate the path of the input image, and the second is for the background. the third argument is used to indicate which way to remove the background. "difference" means using subtraction, while "division" means using the division method. the two methods are stated in the book of < opencv by example >

2017-04-28 08:44:56 -0600 commented question OpenCV Error: Sizes of input arguments do not match when resize

I'm sorry for adding the rest of the codes after so long time for I was engaged in my study. Now I have added all of the lines of my code. Can you see where it goes wrong?

2017-04-15 03:50:53 -0600 asked a question OpenCV Error: Sizes of input arguments do not match when resize

I want to modify the size of the image if the input image(img and pattern)is too big. The input images are img which is the picture including objects and background and the pattern which is the picture of the background. After changing the size, I use pattern to subtract img, which is expected to remove the background of the picture. However, I received "OpenCV Error: Sizes of input arguments do not match ". While, if I do not change the size and just conduct the subtraction the error won't emerge. So, I suspect that there is something wrong with my code which is used to change the size of the input picture. Can you help me find it out?

There is something need to point out and that is I make the program reveal the size of the pictures before and after the resize activity and it seemed to word, but the error is still there and the output image that contain objects only can't be shown. Here is may code which is used to change the size of the p[icture:

Mat removeLight(Mat img, Mat pattern, int method);

int main(int argc,char*argv[])
{
    Mat img=imread(argv[1],0);//input
    Mat pattern= imread(argv[2],0);//background
    //Mat pattern=255*Mat::ones(img.rows,img.cols,CV_8UC1);
    Mat objects;//objects without background
    cout<<"rows:"<<(int)img.rows<<"\t"<<"cols"<<(int)img.cols<<endl;
    int method;//which way to remove the background
    if(img.rows>2048||img.cols>2048)
        {
        resize(img,img,Size((img.cols)/10,(img.rows)/10),CV_INTER_LINEAR);
        resize(pattern,pattern,Size((img.cols)/10,(img.rows)/10),CV_INTER_LINEAR);
        }
    cout<<"rows:"<<(int)img.rows<<"\t"<<"cols"<<(int)img.cols<<endl;
    medianBlur(img,img,7);
    medianBlur(pattern,pattern,7);
    if(!strcmp(argv[3],"difference"))
    {
        method=2;
    }
    else if(!strcmp(argv[3],"division"))
    {
        method=1;
    }
    else
    {
        cout<<"The input is wrong!"<<endl;
        return 0;
    }
    cout<<"method"<<(int)method<<endl;
    objects=removeLight(img, pattern, method);
    imshow("input",img);
    imshow("background",pattern);
    imshow("image without background",objects);
    waitKey(0);
    imwrite("../without.jpg",objects);
    img.release();
    pattern.release();
    objects.release();

}


Mat removeLight(Mat img, Mat pattern, int method)
{
    Mat aux;
    aux= Mat::ones(img.cols,img.rows,CV_8UC1);
    //if method is nomalization
    if(method==1)
    {
        //require change our image to 32 float for division
        Mat img32, pattern32;
        img.convertTo(img32,CV_32FC1);//
        pattern.convertTo(pattern32,CV_32FC1);//
        //Divide the image by the pattern
        aux= 1-(img32/pattern32);
        //Scake it to convert to 8biit format
        aux=aux*255;
        //convert 8 bits format
        aux.convertTo(aux, CV_8UC1);
    }
    else
    {
        Mat mask= img<=pattern;
        subtract(pattern,img,aux,mask);
        aux.setTo(0,1-mask);
    }
    return aux;
}

The hint in the terminal revealed :

rows:4160 cols3120

rows:416 cols312

method2

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same type), nor 'array op scalar', nor 'scalar op array') in compare, file /home/haoxuan/_SOFT/opencv-3.2.0/modules/core/src ... (more)

2017-04-11 05:53:20 -0600 received badge  Editor (source)
2017-04-11 05:51:16 -0600 asked a question How can I divide two matrixes with mask?

I am trying to wipe off the background of a picture and I am trying to use the division method. However, in my picture, some parts of the object reflect light which has higher intensity of the background. The algorithm is result=255*(1-picture/background). I want to conduct division when the intensity of the pixel of the picture is lower than that of the background, while other pixels will be convert into 0. I want to do this with a mask, is there any ready-made function in opencv that can conduct division with a mask?

2017-03-28 08:58:12 -0600 received badge  Supporter (source)
2017-03-28 08:58:10 -0600 received badge  Scholar (source)
2017-03-28 08:57:51 -0600 commented answer Segmentation fault(core dumped) in log transformation

oh! I understood, I confused the rows and the cols. It works when the picture when it is quadratic like lena.jpg. However, it doesn't work when the length isn't as large as width, like the picture of a handwriting. Thank you for your answer~

2017-03-27 09:54:07 -0600 commented answer Segmentation fault(core dumped) in log transformation

Thank you for pointing it out. But what is a quadratic image?

2017-03-27 09:37:43 -0600 commented question Segmentation fault(core dumped) in log transformation

I modified my code as you said, and it works. But there is something I still can not understand: Isn't the rows denoting the number of pixels in a column or the number of rows the Matrix has? And is the First parameter of a Mat class when initializing it represents the numbers of columns?

2017-03-26 10:02:32 -0600 asked a question Segmentation fault(core dumped) in log transformation

I am a beginner of digital image processing. And I want to conduct log transformation for each pixel of the input picture. But, I encountered a segmentation fault(core dumped error). I don't know where does the error exists. Can you help me? For some pictures, like lena.jpg, the error didn't happen, while it happens when I input a picture of a hand-writing on a paper. Here is my code:

Mat Log_Trans(Mat);

int main(int argc,char**argv)
{
      Mat m1=imread(argv[1]);
      if ( m1.empty() )
           {
                  cout<< "Could not open file" <<endl;
                  return ( 1 );
           }
     imshow("Input",m1) ;
     m1=Log_Trans(m1);
     imshow("Log",m1);
    waitKey(0);
    return 1;
}

 Mat Log_Trans(Mat input)
{
       Mat output(input.cols+1,input.rows+1,CV_8UC3,Scalar(0,0,0));
       int i,j;
       for(i=0;i<=input.rows;i++)
             {
                      for(j=0;j<=input.cols;j++)
                          {
                                output.at<Vec3b>(j,i)[0]=(int)log(1+input.at<Vec3b>(j,i)[0]);
                                output.at<Vec3b>(j,i)[1]=(int)log(1+input.at<Vec3b>(j,i)[1]);
                                output.at<Vec3b>(j,i)[2]=(int)log(1+input.at<Vec3b>(j,i)[2]);
                          }
             }
         normalize(output,output,0,255,NORM_MINMAX);
         return output;



}