OpenCV c++ Error trying to load depth file - c++

currently I am working on a program that uses a depth file. However, when I attempt to load this file, I keep getting build errors. I am not getting any error notifications, however, so I am not sure what the problem is. The process of me trying to load the file can be seen below.
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
#define WIDTH 640
#define HEIGHT 480
#define F 524
#define CX 316
#define CY 256
int main()
{
Mat depth;
depth.create(480, 640, CV_16UC1);
FILE *fp = fopen("depth.dat", "rb");
fread(depth.data, 2, WIDTH * HEIGHT, fp);
fclose(fp);
imshow("depth image", depth);
int k = waitKey(0); // Wait for a keystroke in the window
}

Related

SDL2 and KISS_SDL trouble linking on macOS

I am having trouble installing and linking KISS_SDL into my C++ project.
I am running macOS v11.2.3 and have installed SDL2, SDL2_image and SDL2_ttf with brew.
Below is the code in main.cpp that will open a simple SDL2 window:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include "kiss_sdl.h"
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to initialize the SDL2 library\n";
return -1;
}
SDL_Window * window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
680, 480,
SDL_WINDOW_SHOWN);
if(!window)
{
std::cout << "Failed to create window\n";
return -1;
}
SDL_Surface * window_surface = SDL_GetWindowSurface(window);
if(!window_surface)
{
std::cout << "Failed to get the surface from the window\n";
return -1;
}
SDL_UpdateWindowSurface(window);
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}
}
Using the terminal command below I attempt to compile and link the required libraries.
$ g++ -Wall -o main main.cpp -lSDL2 -lSDL2_image -lSDL2_ttf
When I compile without the "#include "kiss_sdl.h"" The program compiles and executes as expected. When I add this include I am getting the following error:
In file included from main.cpp:6:
./kiss_sdl.h:34:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^~~~~~~
1 error generated.
Here is the includes from kiss_sdl.h, I have added a comment in to line 34 where the code is not working:
#ifndef _kiss_sdl_h
#define _kiss_sdl_h
#ifndef RESDIR
#define RESDIR ""
#endif
#if defined(_MSC_VER)
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#elif defined(__APPLE__)
#include <SDL.h> /* Line 34 */
#include <SDL_ttf.h>
#include <SDL_image.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#endif
#include <sys/types.h>
#ifdef _MSC_VER
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <dirent.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
Please look at the issues in the project https://github.com/actsl/kiss_sdl , issue #21 was started based on this. In brief, this is building using a command line only, without using makefile. But the g++ command line that was used is wrong. In Mac, the headers and libraries are provided by framework, but this is missing in the command line. Because of the framework there is also no need to specify SDL2 directory. This has worked for a number of people using Mac, and the framework is specified in the makefile. This was a short answer, it is going to be tested in Mac, but the person doing that is likely busy and has not enough time for that right now.

How to get frames from webcam using OpenCV C++

I'm trying to get frames form a Logitech C920 camera
and show them.
The camera seems to be working but nothing is being shown on the display window.
I tried to configure all the camera settings but nothing.
What am I missing???
#include "pch.h"
#include <iostream>
#include "opencv2\imgcodecs.hpp"
#include "opencv2\core.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\videoio.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture camera(CAP_ANY);
Mat frame;
namedWindow("x", WINDOW_AUTOSIZE);
camera.set(CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G'));
camera.set(CAP_PROP_FRAME_WIDTH, 1920);
camera.set(CAP_PROP_FRAME_HEIGHT, 1080);
while (1)
{
camera.read(frame);
imshow("x", frame);
}
waitKey(0);
return 0;
}
You have to put waitKey(int delay) after each frame. Your while loop should look like this:
while (1)
{
camera.read(frame);
imshow("x", frame);
waitKey(1);
}

OpenCV C++ VS2010: FindContours throws exception

Hi I am trying to run a simple OpenCV program to detect contours in a picture. however, findcontours keeps throwing exceptions.
here is my code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <opencv2\opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
/** #function main */
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <string.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char * argv[]) {
IplImage* img = cvLoadImage("C:\\Users\\310217955\\Documents\\Visual Studio 2010\\Projects\\aviTest\\Debug\\Pictures\\Capture.jpg");
cv::Mat image = cv::cvarrToMat(img);
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
//std::vector<std::vector<cv::Point> > contours;
vector<cv::Mat> contours;
cv::Mat contourOutput = image.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}
cv::imshow("Input Image", image);
cvMoveWindow("Input Image", 0, 0);
cv::imshow("Contours", contourImage);
cvMoveWindow("Contours", 200, 0);
cv::waitKey(0);
system("PAUSE");
return 0;
}
here is an image of the exception:
how can I get around this? is there a dll file im missing or some settings i need to resolve?
I did the following changes to the code
vector<Vec4i> hierarchy;
findContours( contourOutput, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
but now I get the following exception:
Well your code works perfectly fine.I only commented out the header files stdafx.h and nonfree.hpp in my project file . This is the output i gotenter image description here
Make sure you providing the correct path to the input img or paste your input image in your visual studio solution directory and just load the image directly.
The problem may be with one of the modules. I would suggest reinstalling opencv(try downloading the latest version) and try again. Make sure you are adding xyzd.lib files like opencv_highgui330d.lib opencv_imgproc330d.lib
in linker>>Input>> additional dependencies. the version might differ for you.
Hope this helps you!

Undefined reference to '_setargv'

I am new to opencv. While building the basic code, I am getting this error:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main()
{
Mat image;// new blank image
image = cv::imread("test.png", 0);// read the file
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// create a window for display.
imshow( "Display window", image );// show our image inside it.
waitKey(0);// wait for a keystroke in the window
return 0;
}
Error: c:\mingw\bin..\lib\gcc\mingw32\4.8.1......\crt2.0 --- Undefined reference to '_setargv'
Please help me.

Qt + OpenCV grayscale error

I been trying to make a simple interface for an image processing task using OpenCV with C++ using Qt for the GUI.
I'm able to load the image through the GUI but when I press the pushbutton_3,
to convert the image to grayscale gives an error regarding OpenCV.
I'm sure I'm doing something wrong. Can some one give me a help?
Please see below the files:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/imgproc/imgproc.hpp>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
//Images variables
cv::Mat image_Idl;
cv::Mat image_Lit;
cv::Mat image_Idl_G;
cv::Mat image_Lit_G;
double threshHold;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <QSpinBox>
#include <QSlider>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->horizontalSlider->setRange(0,255);
ui->spinBox->setRange(0,255);
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->horizontalSlider,SLOT(setValue(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Load Lit Image"),".",tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
image_Lit = cv::imread(fileName.toAscii().data());
cv::namedWindow("Lit Image");
cv::imshow("Lit Image", image_Lit);
}
void MainWindow::on_pushButton_2_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Load Lit Image"),".",tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
image_Idl = cv::imread(fileName.toAscii().data());
cv::namedWindow("Ideal Lit");
cv::imshow("Ideal Lit", image_Idl);
}
void MainWindow::on_pushButton_3_clicked()
{
//Converstions
//Convert Lit to gray
cv::cvtColor(image_Lit, image_Lit_G,CV_RGB2GRAY);
//Convert Ideal gray
cv::cvtColor(image_Idl, image_Idl_G,CV_RGB2GRAY);
//Threshold the Images to a designated value
// Lit
threshHold = ui->horizontalSlider->value();
cv::threshold(image_Lit_G,image_Lit_G, threshHold,255,cv::THRESH_BINARY);
cv::namedWindow("Gray Scaled Image");
cv::imshow("Gray Scaled Image", image_Lit_G);
}
Error from the compiler:
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file C:\OpenCV\modules\imgproc\src\color.cpp, line 2834
The program has unexpectedly finished.
Pop up window error:
Microsoft Visual C++
This application as requested to terminate in an unusual way.
#include "iostream"
#include "cv.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("C:\\lena.jpg");
cvtColor(image,image,CV_RGB2GRAY);
imshow("test",image);
waitKey();
return 0;
}
above code(console app in Qt..) is working....it shows a gray image...try to include the cv.h file...
I have recompiled the above program after updating my windows7 machine and astonishing it worked!
Its really weird... I didn't made any changes to the program (at least I can't remenber doing anythin significantly different) but after reinstalling comodo firewal it worked.....
Thanks guys, for your help.