I am very new in open cv. I want to display a picture. Here is my code:
#include "stdafx.h"
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( "C:\Users\Cagin\Desktop\New.jpg" );
cvNamedWindow( “Example1”, CV_WINDOW_AUTOSIZE );
cvShowImage( “Example1”, img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( “Example1” );
}
It's like doesn't recognize open cv lib. You can see below my solution window:
As I said before I am very new in open cv. Where is my mistake?
you are dealing with some legacy C code here, are you doing this on purpose? Under the latest builds, this would work for you:
using namespace cv;
int main(int argc, char** argv) {
Mat img = imread("C:\Users\Cagin\Desktop\New.jpg");
namedWindow("Example1", CV_WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
}
If this doesn't work, that means you haven't configured visual studio properly. Try following the instructions here: http://jepsonsblog.blogspot.com/2012/07/installation-guide-opencv-24-with.html
Related
I'm trying to run the code demo for ICU4C bellow, and getting
warning: implicit declaration of function 'austrdup'
which subsequently generate an error. I understand that this is due to the missing imported library that contains 'austrdup' function, and have been looking at the source code to guess which one it is, but no luck. Does anyone have any idea which one should be imported?
#include <unicode/umsg.h>
#include <unicode/ustring.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
UChar* str;
UErrorCode status = U_ZERO_ERROR;
UChar *result = NULL;
UChar pattern[100];
int32_t resultlength, resultLengthOut, i;
double testArgs[] = { 100.0, 1.0, 0.0};
str=(UChar*)malloc(sizeof(UChar) * 10);
u_uastrcpy(str, "MyDisk");
u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
for(i=0; i<3; i++){
resultlength=0;
resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, testArgs[i], str);
if(status==U_BUFFER_OVERFLOW_ERROR){ //check if output truncated
status=U_ZERO_ERROR;
resultlength=resultLengthOut+1;
result=(UChar*)malloc(sizeof(UChar) * resultlength);
u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, testArgs[i], str);
}
printf("%s\n", austrdup(result) ); //austrdup( a function used to convert UChar* to char*)
free(result);
}
return 0;
}
austrdup is not an official ICU method. It's only used by tests in ICU and defined in icu4c/source/test/cintltst/cintltst.h and implemented in icu4c/source/test/cintltst/cintltst.c. It is bascially just a wrapper around u_austrcpy.
This is my first time using FFmpeg. Every type of media file that I try to open with avformat_open_input, returns "Invalid data found when processing input". I am using 32bit FFmpeg Build Version: 92de2c2. I setup my VS2015 project according to this answer: Use FFmpeg in Visual Studio. What could be going wrong with this code?
#include "stdafx.h"
#include <stdio.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
}
int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx = NULL;
avcodec_register_all();
const char* filename = "d:\\a.mp4";
int ret = avformat_open_input(&pFormatCtx, filename, NULL, NULL);
if (ret != 0) {
char buff[256];
av_strerror(ret, buff, 256);
printf(buff);
return -1;
}
}
You forgot to call av_register_all, ffmpeg has no demuxer/muxer registered.
#include "stdafx.h"
#include <stdio.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
}
int main(int argc, char *argv[])
{
AVFormatContext *pFormatCtx = NULL;
av_register_all();
avcodec_register_all();
const char* filename = "d:\\a.mp4";
int ret = avformat_open_input(&pFormatCtx, filename, NULL, NULL);
if (ret != 0) {
char buff[256];
av_strerror(ret, buff, 256);
printf(buff);
return -1;
}
}
Can I use URLOpenBlockingStream on win32 console application ?
I have a simple code:
#include "stdafx.h"
#include <urlmon.h>
#include <iostream>
using namespace std;
bool url(){
LPCWSTR pfile(NULL);
HRESULT hRez = URLDownloadToFile( NULL, _T("www.google.com"), (pfile), 0, NULL );
if( hRez == 0 ){
return 1;
// download ok
}
else{
return 0;// download failed
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<< url();
return 0;
}
and it returns compiler error
All outputs are up-to-date.
1>vvconsole.obj : error LNK2019: unresolved external symbol _URLDownloadToFileW#20 referenced in function "bool __cdecl url(void)" (?url##YA_NXZ)
1>C:\Users\Prem kumar Sekaran\Documents\Visual Studio 2010\Projects\vvconsole\Debug\vvconsole.exe : fatal error LNK1120: 1 unresolved externals
1>Build FAILED.
I am using built in libraries, Do I still need to link the .lib file ?
URLDownloadToFile() requires the header urlmon.h and urlmon.lib library. Add below line to the top of your code to link this library.
#pragma comment(lib, "urlmon.lib")
#include "stdafx.h"
#include <urlmon.h>
#include <iostream>
#pragma comment(lib, "urlmon")
using namespace std;
bool url(){
LPCWSTR pfile(NULL);
HRESULT hRez = URLDownloadToFile( NULL, _T("www.google.com"), (pfile), 0, NULL );
if( hRez == 0 ){
return 1;
// download ok
}
else{
return 0;// download failed
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<< url();
return 0;
}
I am trying to build a program in Opencv that captures a video and saves it to a file. However, after repeated efforts I continue to get this error : Unhandled exception at 0x201e8efb in basic.exe: 0xC0000005 Access violation, where basic.cpp is my file name.
The same occurs when trying to capture an image.
I have already tried changing cvCaptureFromCAM parameters to CV_CAP_ANY/0/-1/-2/1/2..but none of these worked out.My webcam, works perfectly well for others applications and i have tested it online.
Any help would be greatly appreciated .Thanks in advance.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "C:\opencv\build\include\opencv2\core\core.hpp"
#include "C:\opencv\build\include\opencv2\highgui\highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void main( ){
CvCapture *capture = cvCaptureFromCAM( 0 );
int width = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
int height = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
CvVideoWriter *writer = cvCreateVideoWriter( "myCamCapture.avi", -1, 30, cvSize( width, height ) );
cvNamedWindow("camopen", CV_WINDOW_AUTOSIZE);
IplImage *frame = 0;
while( 1 )
{
frame = cvQueryFrame( capture );
cvShowImage("d",frame);
cvWriteFrame( writer, frame );
char c = cvWaitKey( 30 );
if( c == 27 ) break;
}
}
please, since you're a beginner, don't start with the deprecated c-api, use the c++ one instead.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
while( cap.isOpened() )
{
Mat frame;
if ( ! cap.read(frame) )
break;
imshow("lalala",frame);
int k = waitKey(10);
if ( k==27 )
break;
}
return 0;
}
regarding your error: please triple-check the libs you're linking (compiler version, debug/release, 32/64bit). you're not allowed to mix different settings there
I am trying for object detection using opencv 2.4.2. Here is the code for my moving foreground subtraction. Now I want to detect moving object in original frame and draw bounding box around it.
can anybody please help me? how to do that?
#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "conio.h"
#include "time.h"
#include "opencv/cvaux.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
int key = 0;
CvSize imgSize;
CvCapture* capture = cvCaptureFromFile( "S:\\offline object detection database\\TwoEnterShop2cor.MPG" );
IplImage* frame = cvQueryFrame( capture );
imgSize = cvGetSize(frame);
IplImage* grayImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
IplImage* currframe = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
IplImage* destframe = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
if ( !capture )
{
fprintf( stderr, "Cannot open AVI!\n" );
return 1;
}
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );
while( key != 'y' )
{
frame = cvQueryFrame( capture );
currframe = cvCloneImage( frame );// copy frame to current
frame = cvQueryFrame( capture );// grab frame
cvAbsDiff(frame,currframe,destframe);
cvCvtColor(destframe,grayImage,CV_RGB2GRAY);
cvSmooth(grayImage,grayImage,CV_MEDIAN,3,3,0);
cvAdaptiveThreshold(grayImage,grayImage,230,CV_THRESH_BINARY,CV_ADAPTIVE_THRESH_GAUSSIAN_C,3,5);
cvDilate(grayImage, grayImage, 0,1);
cvErode(grayImage,grayImage, 0, 0);
if(key==27 )break;
cvShowImage( "fram",currframe);
cvShowImage( "dest",grayImage);
key = cvWaitKey( 100 );
}
cvDestroyWindow( "dest" );
cvReleaseCapture( &capture );
return 0;
}
Frame difference is the simplest method of background subtraction, but it's very sensitive to threshold you used and may not get good results.The better way is to estimate background model, compare each frame and background model to determine moving objects.
You can find further information from following links:
Background subtraction in OpenCV(C++)
Background Subtraction with OpenCV 2