Add string from arguments to shared memory - memory

I need to add to the shared memory string from arguments (ex. ./a.out abcxyz). I wrote the code, but it don't add string or don't show me string. What is the reason?
int main(int argc, char **argv){
int shmid;
char *buf;
shmid = shmget(KEY, 5, IPC_CREAT | 0600);
buf = (char *)shmat(shmid, NULL, 0);
*buf = argv[1];
printf("\n%c\n", buf);
return 0;
}

You're copying a string, so you can't just use assignment - you need strcpy:
#include <string.h>
...
strcpy(buf, argv[1]);

Related

readlink() error while reading /proc/self/exefile on QNX

I am working on QNX platform, in which I need to get the path of executable which is running.
I have wrote a small peice of code, which is returning always -1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
extern int errno;
int main( int argc, char** argv )
{
char buf[512] = {0};
const char mypath[100] = "/proc/self/exefile";
errno = 0;
printf("The value readlink:: %d %s\n",readlink(mypath, buf, 512 ), strerror( errno ));
return( 0 );
}
When I ran above code then I get following output:
The value readlink:: -1 No such file or directory
Am I missing anything?
What needs to be done to get my current exe path in QNX?
In QNX /proc/self/exefile is not a symbolic link; It's a regular file.
Try:
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char **argv) {
std::ifstream file("/proc/self/exefile");
std::string path;
std::getline(file, path);
std::cout << path << "\n";
return 0;
}

hugepages allocated by mmap is slower than posix_memalign

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <sys/mman.h>
#include <time.h>
#define HUGEPAGE 2048*1024
void *normal_malloc(int len)
{
void *ptr = malloc(len);
bzero(ptr, len);
return ptr;
}
void *trans_malloc(int len)
{
void *ptr = NULL;
int ret = posix_memalign(&ptr, HUGEPAGE, len);
if(ret) perror("posix_memalign");
ret = madvise(ptr, len, MADV_HUGEPAGE);
bzero(ptr, len);
return ptr;
}
void *mmap_malloc(int len)
{
void *ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB, -1,0);
return ptr;
}
int main(int argc, char **argv)
{
char *ptr = NULL;
int len = HUGEPAGE*256;
srand(time(NULL));
switch(argc){
case 1: ptr = normal_malloc(len);break;
case 2: ptr = trans_malloc(len);break;
case 3: ptr = mmap_malloc(len); break;
}
long j = 0;
for(int i=0;i<len;i++){
j += ptr[rand()%len];
}
return 0;
}
I use normal malloc and posix_memalign and mmap to test performance.
My test result is :
malloc cost about 29.7s, posix_memalign cost about 23.5s, and mmap is very near with malloc.
Both posix_memalign and mmap uses hugepages. Why one has obvious improvement, the other not? Do I use mmap in the wrong way?
I don't do bzero for mmap since the man pages says "its contents are initialized to zero".

How can I store a big matrix within the .cc file?

I am currently working on a Computer Vision / Machine Learning project for university. Sadly, they only allow us to upload one single file and restrict the computation time too much. Hence I need to compute the matrices on my machine and store them in the same file as the code (22500 rows, 1 col and 100 rows + 22500 col and 100 rows + 1 col). I already found a way to export the data (link), but I'm not sure how to initialize the matrix.
What I've tried
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char const *argv[])
{
float data[10] = {1,2,3,4,5,7,8,9,10,11};
cv::Mat A;
// Something is wrong with this line
A = cv::Mat(1, 10, cv::CV_32FC1, data);
return 0;
}
When I compile it, I get:
main.cc: In function ‘int main(int, const char**)’:
main.cc:10:16: error: expected primary-expression before ‘(’ token
A = cv::Mat(1, 10, cv::CV_32FC1, data);
^
In file included from /usr/include/opencv2/core/core_c.h:47:0,
from /usr/include/opencv/cv.h:63,
from main.cc:1:
main.cc:10:28: error: expected unqualified-id before ‘(’ token
A = cv::Mat(1, 10, cv::CV_32FC1, data);
^
Second try
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char const *argv[])
{
float dataHeaderMat1[10] = {1,2,3,4,5,7,8,9,10,11};
cv::Mat matrix1;
// Something is wrong with this line
cv::cvInitMatHeader( &matrix1, 10, 1, CV_64FC1, dataHeaderMat1);
return 0;
}
gives
main.cc:10:5: error: ‘cvInitMatHeader’ is not a member of ‘cv’
cv::cvInitMatHeader( &matrix1, 10, 1, CV_64FC1, dataHeaderMat1);
^
The following works to declare and initialize a matrix:
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char const *argv[])
{
float data[10] = {1,2,3,4,5,7,8,9,10,11};
cv::Mat A;
// Something is wrong with this line
A = cv::Mat(1, 10, CV_32FC1, data);
return 0;
}
However, I'm not too sure if this is the best way for big arrays.
You can try to save image to header file, like this:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// uncomment for test
//#include "image.h"
int main(int argc, char **argv)
{
// This part creates header file from image.
Mat img=imread("D:\\ImagesForTest\\lena.jpg");
int w=img.cols;
int h=img.rows;
int channels=img.channels();
ofstream os("image.h");
os << "int rows=" << h << ";" << endl;
os << "int cols=" << w << ";" << endl;
os << "unsigned char d[]={" << endl;
for(int i=0;i<h;++i)
{
for(int j=0;j<w;++j)
{
if(i!=(w-1) || j!=(h-1))
{
Vec3b b=img.at<Vec3b>(i,j);
os << format("0x%02x,",b[0]);
os << format("0x%02x,",b[1]);
os << format("0x%02x,",b[2]);
}
}
}
Vec3b b=img.at<Vec3b>(w-1,h-1);
os << format("0x%02x,",b[0]);
os << format("0x%02x,",b[1]);
os << format("0x%02x",b[2]);
os << endl << "};" << endl;
os << "Mat I=Mat(rows,cols,CV_8UC3,d);" << endl;
os.close();
// To test uncomment commented part of code and comment uncommented.
// uncomment for test
/*
namedWindow("I");
imshow("I",I);
waitKey();
return 0;
*/
}
But be careful, not all IDEs likes such large files.

How do I return calculated Histogram results from OpenCV back to LabVIEW?

I have no problems creating my DLL and sending images back & forth between LabVIEW and OpenCV DLL's.
Here is the code I am working with.
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
// extern C
extern "C" {
_declspec (dllexport) int MyHistCalc(unsigned char *imageIN, int rows, int cols, double threshold1, double threshold2, int kernel_size, unsigned char *imageOUT, unsigned char *HistCalcImageOUT);
}
_declspec (dllexport) int MyHistCalc(unsigned char *imageIN,
int rows,
int cols,
double threshold1,
double threshold2,
int kernel_size,
unsigned char *imageOUT,
unsigned char *HistCalcImageOUT) ...
I am unsure if my problem is catching image_histcalcoutput incorrectly in LabVIEW or returning result correctly.
Please find screenshot of my VI attached.

Compressing IplImage to JPEG using libjpeg in OpenCV

So I have this problem. I have an IplImage that i want to compress to JPEG and do something with it. I use libjpeg8b.The code exit when it goes the function of jpeg_start_compress() with an error of "Bogus input colorspace" .Here are my code.
#include "highgui.h"
#include <stdio.h>
#include "jpeglib.h"
#include "cv.h"
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
#pragma comment(lib, "jpeglib.lib")
bool ipl2jpeg(IplImage *frame, unsigned char **outbuffer, unsigned long*outlen)
{
IplImage *img = new IplImage;
memcpy(img,frame,frame->nSize);
unsigned char *outdata = (uchar *) img->imageData;
struct jpeg_compress_struct cinfo = {0};
struct jpeg_error_mgr jerr;
JSAMPROW row_ptr[1];
int row_stride;
*outbuffer = NULL;
*outlen = 0;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, outbuffer, outlen);
cinfo.image_width = frame->width;
cinfo.image_height = frame->height;
cinfo.input_components = frame->nChannels;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
system("pause");
row_stride = frame->width * frame->nChannels;
while (cinfo.next_scanline < cinfo.image_height)
{
row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return true;
}
int main()
{
ofstream fout("text.txt");
unsigned char **buf;
buf = new unsigned char* [120];
for(int i=0;i<500;i++)
{
buf[i] = new unsigned char[120];
}
for(int i=0;i< 120;i++)
{
for(int j=0;j<120;j++)
{
buf[i][j] = 0;
}
}
unsigned long *len = new unsigned long;
*len = 120*120;
Ptr<IplImage> img = cvLoadImage("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
ipl2jpeg(img,buf,len);
for(int i=0;i< 120;i++)
{
for(int j=0;j<120;j++)
{
fout<<buf[i][j]<<endl;
}
}
return 0;
}
I've never used libjpeg before, but it looks like you're mixing color spaces. You load the image as a grayscale (CV_LOAD_IMAGE_GRAYSCALE), but are telling libjpeg that it's an RGB image (JCS_RGB). Have you tried changing the line
cinfo.in_color_space = JCS_RGB;
to
cinfo.in_color_space = JCS_GRAYSCALE;
?
Is there any reason why you're not using opencv's native JPEG support?
cvSaveImage(frame, "frame.jpeg");
The documentation is here.
EDIT
If you insist on using libjpeg, have a look at this post.

Resources