converting from unsigned char to uchar4 - c++

i'm learning CUDA and i came across a course that is helping even though the code is very old and i'm having problems running it i'm trying to understand it, so he reads images using openCV imread which gives a Mat obj i guess but the data is saved as a uchar*
cv::Mat image = cv::imread(filename.c_str(), CV_LOAD_IMAGE_COLOR);
but after i was stuck in converting uchar to uchar4 and i was reading the code from the teacher and he wrote .
cv::Mat image = cv::imread(filename.c_str(), CV_LOAD_IMAGE_COLOR);
if (image.empty()) {
std::cerr << "Couldn't open file: " << filename << std::endl;
exit(1);
}
cv::cvtColor(image, imageInputRGBA, CV_BGR2RGBA);
//allocate memory for the output
imageOutputRGBA.create(image.rows, image.cols, CV_8UC4);
//This shouldn't ever happen given the way the images are created
//at least based upon my limited understanding of OpenCV, but better to check
if (!imageInputRGBA.isContinuous() || !imageOutputRGBA.isContinuous()) {
std::cerr << "Images aren't continuous!! Exiting." << std::endl;
exit(1);
}
*h_inputImageRGBA = (uchar4 *)imageInputRGBA.ptr<unsigned char>(0);
*h_outputImageRGBA = (uchar4 *)imageOutputRGBA.ptr<unsigned char>(0);
are the two last lines the ones where he subtly converts from uchar to uchar4 ...
h_inputImageRGBA
h_outputImageRGBA
are both uchar4**
can somebody help me understand the code
here is the link to the source
function name : Preprocess

Related

openCV to dlib - Mat to array2d

I'm successfully opening and displaying a .avi video using OpenCV and I need this to go through OpenCV because I want to learn how to make OpenCV and dlib communicate.
For my understanding, a Mat has to be converted into an array2d in order to be processed by dlib so here's my first attempt:
cv::VideoCapture cap("/home/francesco/Downloads/05-1.avi");
cv::namedWindow("UNLTD", CV_WINDOW_AUTOSIZE);
while(1)
{
cv::Mat temp;
cv_image<bgr_pixel> cimg(temp);
std::vector<rectangle> faces = detector(cimg);
cout << faces.size() << endl;
cv::imshow("UNLTD", temp);
}
This returns the error
Error detected in file /usr/local/include/dlib/opencv/cv_image.h.
Error detected in function dlib::cv_image<pixel_type>::cv_image(cv::Mat) [with pixel_type = dlib::bgr_pixel].
Failing expression was img.depth() == cv::DataType<typename pixel_traits<pixel_type>::basic_pixel_type>::depth && img.channels() == pixel_traits<pixel_type>::num.
The pixel type you gave doesn't match pixel used by the open cv Mat object.
img.depth(): 0
img.cv::DataType<typename pixel_traits<pixel_type>::basic_pixel_type>::depth: 0
img.channels(): 1
img.pixel_traits<pixel_type>::num: 3
I tried swapping bgr_pixel to rgb_pixel but without any luck.
Looking around the internet somebody mentioned that the img.depth() is zero, therefore I should use unsigned char instead of rgb_pixel.
First thing: my video is playing in colors, so it does have 3 channels, I don't understand why it should be interpreted as a 1 channel image.
The strange thing is that, making that change from rgb_pixel to unsigned char, makes the software work but ZERO faces are detected on that video stream (that is the video of a guy talking and the face on the same video is detected with no problems by dlib on python.
I don't understand what I'm doing wrong
In your code, the temp is empty because you have not fed any frame from the video capture to it. Conversion of cv::Mat to dlib::array2d is also not correct. Please see this post for more information.
You may try:
cv::VideoCapture cap("/home/francesco/Downloads/05-1.avi");
cv::namedWindow("UNLTD", CV_WINDOW_AUTOSIZE);
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
while(1)
{
cv::Mat temp;
cap >> temp;
dlib::array2d<bgr_pixel> dlibFrame;
dlib::assign_image(dlibFrame, dlib::cv_image<bgr_pixel>(temp));
std::vector<rectangle> faces = detector(dlibFrame);
cout << faces.size() << endl;
cv::imshow("UNLTD", temp);
}

Machine Vision 8bit Bayer RGGB to RGB in Open CV

I am currently trying to convert an 8bit Raw color image to 8 bit RGB.
i get a channel error it expect the Bayer to be 1 channel.
i am using the following code.
if (convertBayerChckBox->Checked)
{
try{
cv::Mat temp(imgOriginal.rows, imgOriginal.cols, CV_8UC3);
imgOriginal.copyTo(temp);
cv::cvtColor(temp, imgOriginal, CV_BayerRG2BGR);
}
catch (const cv::Exception& ex)
{
std::cout << "EXCEPTION: " << ex.what() << std::endl;
errLog << "EXCEPTION: " << ex.what() << std::endl;
}
}
then i am getting the following exception:
EXCEPTION: ......\modules\imgproc\src\color.cpp:4194: error: (-215) scn == 1 && dcn == 3 in function cv::cvtColor
I am not sure how else to convert from Bayer to RGB
You need to set the data pointer of your input Mat to your destination pointer.
Here is a sample program to convert your bayer image to RGB. Here I've used buffer from a file. You can use your camera frame buffer. Hope this helps!
Mat mSource_Bayer(Size(m_IWidth,m_IHeight),CV_8UC1);
Mat mSource_Bgr(Size(m_IWidth,m_IHeight),CV_8UC3);
FILE *fp = NULL;
uchar *imagedata = NULL;
int framesize = m_IWidth * m_IHeight;
//Open raw Bayer image.
fp = fopen(FileName_S.c_str(), "rb");
//Memory allocation for bayer image data buffer.
imagedata = (uchar*) malloc (sizeof(uchar ) * framesize);
//Read image data and store in buffer.
fread(imagedata, sizeof(uchar ), framesize, fp);
mSource_Bayer.data= imagedata;
fclose(fp);
int Selection= m_BayerFormat.GetCurSel();
if(Selection==0)
cvtColor(mSource_Bayer, mSource_Bgr, CV_BayerBG2BGR);//Perform demosaicing process
else if(Selection==1)
cvtColor(mSource_Bayer, mSource_Bgr, CV_BayerGB2BGR);//Perform demosaicing process
else if(Selection==2)
cvtColor(mSource_Bayer, mSource_Bgr, CV_BayerRG2BGR);//Perform demosaicing process
else if(Selection==3)
cvtColor(mSource_Bayer, mSource_Bgr, CV_BayerGR2BGR);//Perform demosaicing process
imshow("mSource_Bgr",mSource_Bgr);
Also in order to perform BAYER to RGB conversion I would recommend to use function SimdBayerToBgr from Simd Library. It seems that it function is faster than its analogue from OpenCV (it uses AVX2 and AVX-512).

Opening images one after the other

I have a series of images saved on my system according to their time stamps.
For example the images are named as:
20140305180348.jpg
20140305180349.jpg
I have 100 such images, I want to open them using OpenCV one after the other. I have tried using cvCapturefromFile() but using it I am able to open just a single image at a time. I want to stitch/join them so that I can make a video.
I am sorry I cannot post the code as I am not allowed to. How do I proceed?
In OpenCV, to write images to a video, you can use VideoWriter (and do it in a loop to read a sequence of images):
VideoWriter outputVideo; // Open the output
// ... set video properties like FPS
if (!outputVideo.isOpened())
{
cout << "Could not open the output video for write: " << source << endl;
return -1;
}
for(...)
{
// read your frame, e.g. to Mat img
// outputVideo.write(img); //save or
outputVideo << img;
}
cout << "Finished writing" << endl;
Check out here for more info.
In this tutorial is an example how to write a video. Just modify the for-loop in the end.
pseudocode:
open videocontainer
int i=0;
while(i<100){
Mat img = imread("path"+to_string(i)+".jpg");
outputvideo << img;
}
close videocontainer

Open Cv fisherfaces

I have this problem,
when i run with vs2010 (debug) (open cv 2.4.0) facerec_demo.cpp gaves me the program this error
OpenCV Error: Image step is wrong (The matrix is not continuous, thus its number of rows can not be changed) in unknown function, file ......\src\opencv\modul es\core>\src\matrix.cpp, line 801
This error derives me to this line in facerec.cpp
(Fisherfaces::train(InputArray src, InputArray _lbls)
Mat data = asRowMatrix(src, CV_64FC1); <-- this gets a exeption, not handled.
and a i use at pgm img database and this is my original *facerec_demo.cpp* file
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
vector<string> split_at_commas(const string& row)
{
vector<string> res;
istringstream buf(row);
string s;
while (getline(buf, s, ';'))
res.push_back(s);
return res;
}
Mat toGrayscale(InputArray _src) {
Mat src = _src.getMat();
// only allow one channel
if(src.channels() != 1)
CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
// create and return normalized image
Mat dst;
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
return dst;
}
void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
//std::ifstream file(filename.c_str(), ifstream::in);
std::ifstream file(_T("D:\\Users\\PC ACER\\Documents\\mycsv4.csv"));
if (!file)
throw std::exception();
string line="", path="", classlabel="";
while (getline(file, line)) {
//vector<string> values = split_at_commas(line);
stringstream liness(line);
getline(liness, path, ';');
getline(liness, classlabel);
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
int main(int argc, const char *argv[]) {
// check for command line arguments
if (argc != 2) {
cout << "usage: " << argv[0] << " <csv.ext>" << endl;
exit(1);
}
// path to your CSV
string fn_csv = string(argv[1]);
// images and corresponding labels
vector<Mat> images;
vector<int> labels;
// read in the data
try {
read_csv(fn_csv, images, labels);
} catch (exception&) {
cerr << "Error opening file \"" << fn_csv << "\"." << endl;
exit(1);
}
// get width and height
//int width = images[0].cols;
int height = images[0].rows;
// get test instances
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
// ... and delete last element
images.pop_back();
labels.pop_back();
// build the Fisherfaces model
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
// test model
int predicted = model->predict(testSample);
cout << "predicted class = " << predicted << endl;
cout << "actual class = " << testLabel << endl;
// get the eigenvectors
Mat W = model->eigenvectors();
// show first 10 fisherfaces
for (int i = 0; i < min(10, W.cols); i++) {
// get eigenvector #i
Mat ev = W.col(i).clone();
// reshape to original size AND normalize between [0...255]
Mat grayscale = toGrayscale(ev.reshape(1, height));
// show image (with Jet colormap)
Mat cgrayscale;
applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
imshow(format("%d", i), cgrayscale);
}
waitKey(0);
return 0;
}
I see you are using OpenCV 2.4.0. As the developer I admit the confusion is my fault: I didn't thoroughly check the input data passed to the training method back then, so people passing wrongly aligned data got error messages like yours. Most likely the error you see happens, because your training images don't have equal size. This is necessary for the Eigenfaces and Fisherfaces algorithm (not for the Local Binary Patterns Histograms). OpenCV 2.4.0 just tries to reshape the data to a matrix and blows up with the error message you see; OpenCV 2.4.2 instead checks (before training) if the input data is correctly aligned and throws a meaningful exception... with a very clear message.
This post assumes it could also be due to linking the OpenCV libraries:
Getting OpenCV Error "Image step is wrong" in Fisherfaces.train() method
If it's not linking the libraries it might be due to the image size. Resizing your training images, can easily be done OpenCV with cv::resize:
http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html#resize
But you probably should consider to switch to OpenCV 2.4.2, where all this is added:
http://opencv.org/opencv-v2-4-2-released.html
This version also comes with an extensive documentation at:
http://docs.opencv.org/trunk/modules/contrib/doc/facerec/index.html
However if you can't change to OpenCV 2.4.2 and you'll need to stay with OpenCV 2.4.0, then you could also use libfacerec:
https://github.com/bytefish/libfacerec
This is the project, that got merged into OpenCV. I made sure it works with OpenCV 2.4.0 and it'll leave you with exactely the same interface as the OpenCV 2.4.2 version. So once you feel like updating to OpenCV 2.4.2, you'll only switch the includes.
I got the same OpenCv error, I try all help that I find here, and it still gives me an exception (exception happend on .Predict() statement).
Problem was in the size of images. Size of an Images must be less then 100px (<100px) (not sure if exactly less then 100,maybe 100 would still work).
I change my pictures size of 150:150 to 80:80 and its working!
Hope I help someone, because this was annoying error.
I answered this question on another post but I want to make sure people searching for help with this error are sure to find the answer.
when you make the model
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
You need to pass two params
createFisherFaceRecognizer(int num_components=0, double threshold=DBL_MAX);
This page has more information on how createFisherFaceRecognizer works

OpenCV2.1, map function? accessing each pixel?

I have a function that I would like to apply to each pixel in a YUN image (call it src). I would like the output to be saved to a separate image, call it (dst).
I know I can achieve this through pointer arithmetic and accessing the underlying matrix of the image. I was wondering if there was a easier way, say a predefined "map" function that allows me to map a function to all the pixels?
Thanks,
Since I don't know what a YUN image is, I'll assume you know how to convert RGB to that format.
I'm not aware of an easy way to do the map function you mentioned. Anyway, OpenCV has a few predefined functions to do image conversion, including
cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
which you might want to take a closer look.
If you would like to do your own, you would need to access each pixel of the image individually, and this code shows you how to do it (the code below skips all kinds of error and return checks for the sake of simplicity):
// Loading src image
IplImage* src_img = cvLoadImage("input.png", CV_LOAD_IMAGE_UNCHANGED);
int width = src_img->width;
int height = src_img->height;
int bpp = src_img->nChannels;
// Temporary buffer to save the modified image
char* buff = new char[width * height * bpp];
// Loop to iterate over each pixel of the original img
for (int i=0; i < width*height*bpp; i+=bpp)
{
/* Perform pixel operation inside this loop */
if (!(i % (width*bpp))) // printing empty line for better readability
std::cout << std::endl;
std::cout << std::dec << "R:" << (int) src_img->imageData[i] <<
" G:" << (int) src_img->imageData[i+1] <<
" B:" << (int) src_img->imageData[i+2] << " ";
/* Let's say you wanted to do a lazy grayscale conversion */
char gray = (src_img->imageData[i] + src_img->imageData[i+1] + src_img->imageData[i+2]) / 3;
buff[i] = gray;
buff[i+1] = gray;
buff[i+2] = gray;
}
IplImage* dst_img = cvCreateImage(cvSize(width, height), src_img->depth, bpp);
dst_img->imageData = buff;
if (!cvSaveImage("output.png", dst_img))
{
std::cout << "ERROR: Failed cvSaveImage" << std::endl;
}
Basically, the code loads a RGB image from the hard disk and performs a grayscale conversion on each pixel of the image, saving it to a temporary buffer. Later, it will create another IplImage with the grayscale data and then it will save it to a file on the disk.