Ask Your Question

jrpharis's profile - activity

2014-10-03 11:32:28 -0600 received badge  Famous Question (source)
2014-03-16 17:49:25 -0600 received badge  Notable Question (source)
2013-11-07 09:34:50 -0600 received badge  Popular Question (source)
2013-04-13 20:57:15 -0600 asked a question videowriter: integer division by zero error

I am using the sample code video-write.cpp(included below for convenience) to learn how to use videos with OpenCV.


#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2 core="" core.hpp="">        // Basic OpenCV structures (cv::Mat)
#include <opencv2 highgui="" highgui.hpp="">  // Video write

using namespace std;
using namespace cv;

static void help()
{
    cout
        << "------------------------------------------------------------------------------" << endl
        << "This program shows how to write video files."                                   << endl
        << "You can extract the R or G or B color channel of the input video."              << endl
        << "Usage:"                                                                         << endl
        << "./video-write inputvideoName [ R | G | B] [Y | N]"                              << endl
        << "------------------------------------------------------------------------------" << endl
        << endl;
}

int main(int argc, char *argv[])
{
    help();

    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source      = argv[1];           // the source file name
    const bool askOutputType = argv[3][0] =='Y';  // If false it will use the inputs codec type

    VideoCapture inputVideo(source);              // Open input
    if (!inputVideo.isOpened())
    {
        cout  << "Could not open the input video: " << source << endl;
        return -1;
    }

    string::size_type pAt = source.find_last_of('.');                  // Find extension point
    const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container
    int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form

    // Transform from int to char via Bitwise operators
    char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};

    Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    // Acquire input size
                  (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));

    VideoWriter outputVideo;                                        // Open the output
    if (askOutputType)
        outputVideo.open(NAME, ex=-1, inputVideo.get(CV_CAP_PROP_FPS), S, true);
    else
        outputVideo.open(NAME, ex, inputVideo.get(CV_CAP_PROP_FPS), S, true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write: " << source << endl;
        return -1;
    }

    cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height
         << " of nr#: " << inputVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;
    cout << "Input codec type: " << EXT << endl;

    int channel = 2; // Select the channel to save
    switch(argv[2][0])
    {
    case 'R' : channel = 2; break;
    case 'G' : channel = 1; break;
    case 'B' : channel = 0; break;
    }
    Mat src, res;
    vector<mat> spl;

    for(;;) //Show the image captured in the window and repeat
    {
        inputVideo >> src;              // read
        if (src.empty()) break;         // check if at end

        split(src, spl);                // process - extract only the correct channel
        for (int i =0; i < 3; ++i)
            if (i != channel)
                spl[i] = Mat::zeros(S, spl[0].type());
       merge(spl, res);

       //outputVideo.write(res); //save or
       outputVideo << res;
    }

    cout << "Finished writing" << endl;
    return 0;
}

However, once the program starts to write to the output file, 3 lines from the bottom of the code, I get the error below. I get the same error using either output method, and if I try to just put the src straight back to the output(outputVideo << src).

I found another sample program(http://opencv.willowgarage.com/wiki/documentation/cpp/highgui/VideoWriter) that opens the default camera(my built-in webcam), it is able to output just fine. This code is below.

    
    #include "opencv\cv.h"
#include "opencv\highgui.h"

using namespace cv;

int main(int, char**)
{
    VideoCapture capture(1); // open the default camera

    if ...
(more)
2013-03-27 14:25:11 -0600 received badge  Self-Learner (source)
2013-03-17 16:59:15 -0600 commented answer Unhandled exception using cvCanny

Yes, I completely agree. I have also used Mat, my teacher sets up the main and for some reason uses IplImage. The first thing I do is convert that to Mat. But the cvCanny function requires the input and output containers be IplImage. I tried with cvPyrSegmentation, which also requires IplImage*, to use casted Mat structures but got errors. I have not tried that with cvCanny yet.

2013-03-17 15:19:24 -0600 asked a question Unhandled exception using cvCanny

I am implementing cvCanny for a segmetation/edge detection project for my image processing class. The program runs multiple segmentation algorithms via command line arguments, and passes the input image to the corresponding algorithm via IplImage*. You can see in the code below, I am taking the pointer passed to the function, and creating a local copy to do the work on.

void CannyEdgeSeg (IplImage * image, int)

{ const char* name = "Edge Detection Window"; // Kernel size int N = 7;

// Set up images
IplImage* img;
img = cvCloneImage(image);
IplImage* img_b = cvCreateImage( cvSize(img->width+N-1,img->height+N-1), img->depth, img->nChannels );
IplImage* out = cvCreateImage( cvGetSize(img_b), IPL_DEPTH_8U, img_b->nChannels );


// Add convolution boarders
CvPoint offset = cvPoint((N-1)/2,(N-1)/2);
cvCopyMakeBorder(img, img_b, offset, IPL_BORDER_REPLICATE, cvScalarAll(0));

// Make window
cvNamedWindow( name, 1 );

// Edge Detection Variables
int aperature_size = N;
double lowThresh = 20;
double highThresh = 40;

// Create trackbars
cvCreateTrackbar( "High", name, &high_switch_value, 4, switch_callback_h );
cvCreateTrackbar( "Low", name, &low_switch_value, 4, switch_callback_l );

switch_callback_h(0);
switch_callback_l(0);

for(;;) {
    switch( highInt ){
        case 0:
            highThresh = 200;
            break;
        case 1:
            highThresh = 400;
            break;
        case 2:
            highThresh = 600;
            break;
        case 3:
            highThresh = 800;
            break;
        case 4:
            highThresh = 1000;
            break;
    }
    switch( lowInt ){
        case 0:
            lowThresh = 0;
            break;
        case 1:
            lowThresh = 100;
            break;
        case 2:
            lowThresh = 200;
            break;
        case 3:
            lowThresh = 400;
            break;
        case 4:
            lowThresh = 600;
            break;
    }

    // Edge Detection
    cvCanny( img_b, out, lowThresh*N*N, highThresh*N*N, aperature_size );
    cvShowImage(name, out);

    if( cvWaitKey( 15 ) == 27 ) 
        break;
}

// Release
cvReleaseImage( &img );
cvReleaseImage( &img_b );
cvReleaseImage( &out );
cvDestroyWindow( name );

};

However, with this methodology, I get this error once it tries to run cvCanny:

Unhandled exception at 0x000007fefd899e5d in Project2.exe: Microsoft C++ exception: cv::Exception at memory location 0x0012f630..

But, if I explicitly call the image to read in my function, rather than using the passed image,

 IplImage* img = cvLoadImage("fruits.jpg", 0);

the application runs just fine.

I don't understand why it would matter if the image is passed or called explicitly in the function. I was able to usethis exact same method, creating a clone of the passed image, in my other segmentation algorightms without any issues. Is there something specific to cvCanny that I am miss? Any help here would be greatly appreciated. Thanks.

2013-02-10 10:51:57 -0600 asked a question Passing IplImage* to function, original not getting updated

I am having trouble passing IplImage* parameters in functions. This is for a school project and unfortunately am not supposed to edit the files doing the calling to my filter functions. In the main, the image is brought in as a command line parameter, then a copy of this is made and passed to the correct filter function, as such:

IplImage * floating = cvCreateImage (cvSize (img->width, img->height), IPL_DEPTH_32F, 3);
cvConvertScale (img, floating, 1/255., 0);

IplImage * filtered;

switch (filter.ImageFormat)
{
  case YUV24:
    filtered = cvCreateImage (cvSize (floating->width, floating->height), IPL_DEPTH_32F, 3);
    cvCvtColor (floating, filtered, CV_BGR2YCrCb);
    break;
  case BGR24:
    filtered = cvCloneImage (floating);
    break;
  case Gray:
    filtered = cvCreateImage (cvSize (floating->width, floating->height), IPL_DEPTH_32F, 1);
    cvCvtColor (floating, filtered, CV_BGR2GRAY);
    break;
}

cvNamedWindow ("original", CV_WINDOW_AUTOSIZE);
cvShowImage ("original", img);

filter.ImageFilter (filtered, p);

if (filter.ImageFormat == YUV24)
{
  cvCvtColor (filtered, filtered, CV_YCrCb2BGR);
}

cvNamedWindow (filterName.c_str(), CV_WINDOW_AUTOSIZE);
cvShowImage (filterName.c_str(), filtered);

And here is the code from one of my filters:

void median (IplImage * image, int k){
  Mat matImage(image);        //convert IplImage to Mat for work
  vector<Mat> bgr_channels;   //vector to hold each channel
  int i,j,m,n;                //row/column indeces
  int kernelSize = (2*k+1)*(2*k+1);
  vector<float> vals(kernelSize); //kernelSized vector to hold all values of image
                                  //within kernel centered at a given pixel
  int vecIndex = 0;   //then sorted to get the median
  int chanIndex = 0;  //index used to for each channel

  //add padding to account for border issues with convolution
  copyMakeBorder( matImage, matImage, k, k, k, k, BORDER_REPLICATE);
  //split channes to do work on individual channels
  split(matImage, bgr_channels);

  for(chanIndex=0; chanIndex < matImage.channels(); chanIndex++){
    //outer loop for scanning entire image
    for(i=k; i<matImage.rows-k; i++)/*image row index*/{
      for(j=k; j<matImage.cols-k; j++)/*image column index*/{
        //inner loop for scanning image only in kernel boundaries
        vecIndex = 0;   //reset vecIndex at start of each kernel loop
        for(m=i-k; m<(i+k+1); m++)/*kernel row index*/{
          for(n=j-k; n<(j+k+1); n++)/*kernel column index*/{                      
            vals[vecIndex++] = bgr_channels[chanIndex].at<float>(m,n);
          }
        }
        insertionSort(vals, 0, vals.size()-1);
        bgr_channels[chanIndex].at<float>(i,j) = vals[vals.size()/2]; 
          //new value chosen from middle element of sorted vector
      }
    }
  }

  merge(bgr_channels, matImage);      //merge channels together
  imshow("Median: Mat", matImage);    //left this in because the original doesn't get modified                                
                                      //when converting the Mat back to an IplImage 
  image = cvCloneImage(&(IplImage)matImage);  //convert backto IplImage for DesktopMain
}

The problem is, that the filtered image shown in the main, does not reflect the actual image. It only shows the original image. When I output put the Mat style image matImage in my filter function, it shows the filtered image. Immediately after I convert back to IplImage and set the IplImage* input parameter equal to the the converted, filtered version. But the changes do not reflect the image displayed in the main function.

This is making it hard to create some of my other filters such as Gaussian and Sobel, because those filters themselves make calls ... (more)

2013-02-10 10:39:00 -0600 answered a question Missing opencv_core243d.dll

I was able to get it going. It turns out I was only adding system variables, I didn't have access on my company laptop to change the path directly. I used the free program Path Editor and everything worked great.

2013-02-04 23:22:48 -0600 commented question Missing opencv_core243d.dll

UPDATE: I went ahead and tried everything with the x64 directories even though my version of VS2010 is 32 bits and when I start debugging it seems to have taken care of the missing opencv_243d.dll, but now I get all kinds of linker errors:

error LNK2019: unresolved external symbol _cvWaitKey referenced in function _main

error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV_InputArray@1@@Z) referenced in function _main

And 25 more just as cryptic. If I have the project pointing to the correct directories and lib files, why can't it find them?

2013-02-04 22:54:15 -0600 asked a question Missing opencv_core243d.dll

I am using a 32 bit version of VS2010 on W7 64 bit OS. I have installed opencv2.4.3 to my root directory C:. I am having issues with VS seeing the .lib files. When I try to debug I get the error:

"The program can't start because opencv_core243.dll is missing from your computer. Try reinstalling the program to fix this problem."

I have added C:\opencv\build\x86\vc10\bin to my system path using Windows' 'Edit environment variables for your account' utility. I have added a property sheet to Win32 | Debug where under Common Properties > C/C++ > General I added C:\opencv\build\include to the Additional Include Directories. Then under Common Properties > Linker > General I added C:\opencv\build\x86\vc10\lib to Additional Library Directories, then all of the *d.lib files from this directory to the Additional Dependencies under Linker > Input.

Is there something I am missing to get the binaries on the system path? Any help would be greatly appreciated. I have been fighting with this for a week now! Thanks.