Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

Wednesday, May 8, 2013

Could not open Video in Windows with OpenCV precompiled dlls

Issue as described in the title. 
The reason is simple, the system need opencv_ffmpeg2**.dll to read video. Just add the bin folder into system path, or directly copy this .dll file into the folder where the executable file generated.

Tuesday, February 26, 2013

OpenCV combine two images

In computer vision community, developer always face one basic task to visualize the correspondences between two frames, and the easier solution might be combine two images into one single image.

In this blog, I will give a convenient and easy to understand solution to solve this problem. Of course, so far, this is just a simple starting point, you could extent it to make more robust.

The code would be as follow:


struct myexception : public std::exception{
    std::string s;
    myexception(std::string ss):s(ss){}
    virtual const char* what() const throw() {return s.c_str();}
    ~myexception() throw(){}
};



// combine two identical size images into one big image
void combineTwoImages(cv::Mat &dst, const cv::Mat img1, const cv::Mat img2){
    if (img1.cols != img2.cols || img1.rows != img2.rows) {
        throw myexception("img1 and img2 should be identical size");
    }
    int rows = img1.rows;
    int cols = img1.cols;
    dst = cvCreateMat(rows, 2 * cols, img1.type());
    cv::Mat tmp = dst(cv::Rect(0, 0, cols, rows));
    img1.copyTo(tmp);
    tmp = dst(cv::Rect(cols, 0, cols, rows));
    img2.copyTo(tmp);
}


As you could notice, this code would only work for identical size case, you would extent it to handle two arbitrary size images as well.

PS: cv::Rect(x, y, _width, _height)

Hope this would be helpful.

Saturday, February 23, 2013

OpenCV: compare whether two Mat is identical

As MATLAB users could always conveniently compare two matrix, in order to decide whether the two matrix is identical or not.
However, in OpenCV this easy task is not straight forward as you first thought.
The underlying small openCV script could help you to address this simple task.

// return true if two matrix is identical, false otherwise


bool BasicUtil::matIsEqual(const cv::Mat mat1, const cv::Mat mat2){
    // treat two empty mat as identical as well
    if (mat1.empty() && mat2.empty()) {
        return true;
    }
    // if dimensionality of two mat is not identical, these two mat is not identical
    if (mat1.cols != mat2.cols || mat1.rows != mat2.rows || mat1.dims != mat2.dims) {
        return false;
    }
    cv::Mat diff;
    cv::compare(mat1, mat2, diff, cv::CMP_NE);
    int nz = cv::countNonZero(diff);
    return nz==0;
}


PS: I'm currently building a C++ toolbox in order to replace some basic MATLAB functionality for computer vision users. I will release it once I built a considerable functionalities.

Friday, October 19, 2012

XCode 4.5.1 linking error with Opencv 2.4.2_1

I always keep my software updated to the latest version. Generally, it works perfectly fine. However, after I upgrade Xcode into 4.5.1 and Opencv to 2.4.2_1 though macports. The simple opencv 2.0 style compile will keep pushing out linking errors.

The solution is the attached image below, switch the selected item from libc++ (LLVM) into libstdc++(GNU C++ standard library)

Hope this is helpful.


Monday, September 24, 2012

mexopencv compile error in MacOSX 10.8

I had the following error message, at the end of this post, when I installed the mexopencv on my MacOSX 10.8 with Xcode 4.5, opencv installed from macports, and config_pkg properly installed.

First of all, I grab the patch from the following link to connect Matlab mex with Xcode,

https://www.mathworks.com/support/solutions/en/data/1-FR6LXJ/

Second, I modify the MACOSX_DEPLOYMENT_TARGET=10.5 into 10.6 or higher.

After these steps, you should be able to compile and enjoy the mexopencv. 

PS: website of mexopencv, (many thanks to kota)
http://www.cs.sunysb.edu/~kyamagu/mexopencv/

/Applications/MATLAB_R2012a.app/bin/mex -c -cxx -largeArrayDims -Iinclude -I/opt/local/include/opencv -I/opt/local/include   src/MxArray.cpp -outdir lib
/opt/local/include/opencv2/flann/params.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
/opt/local/include/opencv2/flann/params.h:87:   instantiated from here
/opt/local/include/opencv2/flann/params.h:87: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
/opt/local/include/opencv2/flann/params.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
/opt/local/include/opencv2/flann/params.h:87:   instantiated from here
/opt/local/include/opencv2/flann/params.h:87: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
/opt/local/include/opencv2/flann/params.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
/opt/local/include/opencv2/flann/params.h:87:   instantiated from here
/opt/local/include/opencv2/flann/params.h:87: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
/opt/local/include/opencv2/flann/params.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
/opt/local/include/opencv2/flann/params.h:87:   instantiated from here
/opt/local/include/opencv2/flann/params.h:87: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available
/opt/local/include/opencv2/flann/params.h: In instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
/opt/local/include/opencv2/flann/params.h:87:   instantiated from here
/opt/local/include/opencv2/flann/params.h:87: error: explicit instantiation of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' but no definition available

   mex: compile of ' "src/MxArray.cpp"' failed.

make: *** [lib/libMxArray.a] Error 1

Monday, September 10, 2012

resize Image in OpenCV

This is simple. Just in case people may need it sometimes.
Here, we resize the image into 1 quarter of the original image, with half height and width comparing to original image.


#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, const char * argv[])
{
    // Read original image into varialbe img
    cv::Mat img = cv::imread("/Users/herbert19lee/Desktop/lena1.jpg");
    // Declare variable img_resize as container for resized image
    cv::Mat img_resize;

    // resize the original img, as half size of original cols and rows
    // cv::Size2i(img.cols/2, img.rows/2) is where you specify the desired dimension for resized image
    cv::resize(img, img_resize, cv::Size2i(img.cols/2, img.rows/2));
  
    // Visualize the original and resized image in window: lena and lena_resize respectively. 
    cv::namedWindow("lena");
    cv::namedWindow("lena_resize");
    cv::imshow("lean", img);
    cv::imshow("lena_resize", img_resize);
    
    cv::waitKey();
    return 0;
}

prettify