IplImage *img;
img = (IplImage **)malloc(IMAGE_NUM * sizeof(IplImage *));
for(index=0; index<IMAGE_NUM; index++){
sprintf(filename, "preproc/preproc%d.jpg", index);
img = cvLoadImage(filename, 0);
}
Hi! This piece of code here produces the error: cannot convert ‘IplImage** {aka _IplImage*}’ to ‘IplImage {aka _IplImage*}’ in assignment. I am trying to load multiple images here. What am I doing wrong? Thanks!
You declare 'img' to be a pointer to IplImage and then you're trying to convert it into pointer to a pointer. (IplImage**) - This typecast is incorrect for this particular case since you're trying to assign IplImage** to a IplImage*.
Declare img to be : IplImage **img;
try this
IplImage** img;
img = (IplImage**)malloc(IMAGE_NUM * sizeof(IplImage *));
for(index=0; index<IMAGE_NUM; index++){
sprintf(filename, "preproc/preproc%d.jpg", index);
*img = cvLoadImage(filename, 0);
}
by the way, the next error you'll get will be for not advancing the img pointer after each loop iteration
Try declaring IplImage** img;, then img[index] = cvLoadImage(filename, 0), since img is an array of IplImage pointers and cvLoadImage() returns a single image.
Related
I'm really noob on processing and programing and I can't figure it out how to show my images at random.
I'm loading the images in setup with the PImage name img0, img1, img2 and then
image("img" + random(3), 0, 0);
But it does't work, coz processing wait for a PImage argument, and the string plus a number isn't.
And I know for shure there must be some better way than:
int randomNumber = random(3);
if(randomNumber == 0 ){
image(img0,0,0);
}
if(randomNumber == 1 ){
image(img1,0,0);
}
if(randomNumber == 2 ){
image(img2,0,0);
}
But I haven't found it.
Any thoughts?
thanks!
In addition to Kevin's great answer you can also use a an array to store the loaded PImages.
Here's a rough example (you'll need to adjust path to images of course):
// total number of images
int numImages = 3;
// an array of images
PImage[] images = new PImage[num];
int randomNumber;
void setup(){
//TODO correct sketch size
size(300,300);
// initialize images array (loading each one)
for(int i = 0 ; i < numImages; i++){
// TODO correct path to images
images[i] = loadImage("img"+(i)+".png");
}
}
void draw(){
background(0);
//render the most recently selected random index image
image(images[randomNumber]);
//instructions
text("click to randomize",10,15);
}
// change the random number on click (draw() would look chaotic/hard to debug)
void mousePressed(){
// pick a random number and cast the floating point value return to integer needed as in images array index
randomNumber = (int)random(numImages);
}
You could use a HashMap to create a map from String keys to PImage values. Something like this:
HashMap<String, PImage> imageMap = new HashMap<String, PImage>();
imageMap.put("image1", image1);
imageMap.put("image2", image2);
Then to get a PImage from a String key, you'd call the get() function:
PImage image1 = imageMap.get("image1");
You can find more info in the reference.
By the way, this line won't compile:
int randomNumber = random(3);
The random() function returns a float value. You can't store a float value in an int variable. You have to convert it using the int() function:
int randomNumber = int(random(3));
If you still can't get it working, please post a MCVE that demonstrates the problem. Good luck.
I'm trying to use BITMAPV5HEADER with CreateDIBSection to get a uint8[] in form of RGBA. I am doing this from ctypes but this isn't a ctypes question but I'll post the code so you can see it. The ctypes guys won't know whats up here it's a winapi thing.
I double checked my struct, function, and type declares but I keep getting GetLastError of 87 which is invalid parameter after calling CreateDIBSection. If I set the bv5Size to the size of BITMAPINFOHEADER which is 40 it works but is treated as BITMAPINFO header and the red,blue,green,alpha masks dont affect it, as I still get BGRA.
So I was wondering what is the size supposed to be for BITMAPV5HEADER on 32bit and 64bit please. For me I'm getting 124:
"ostypes.TYPE.BITMAPINFOHEADER.size:" 40
"ostypes.TYPE.BITMAPV5HEADER.size:" 124
This is my ctypes code just to show that everything is correct:
var bmi = ostypes.TYPE.BITMAPV5HEADER();
bmi.bV5Size = ostypes.TYPE.BITMAPV5HEADER.size;
bmi.bV5Width = nWidth; //w;
bmi.bV5Height = -1 * nHeight; //-1 * h; // top-down
bmi.bV5Planes = 1;
bmi.bV5BitCount = nBPP; //32;
bmi.bV5Compression = ostypes.CONST.BI_BITFIELDS;
bmi.bV5RedMask = ostypes.TYPE.DWORD('0x00FF0000');
bmi.bV5GreenMask = ostypes.TYPE.DWORD('0x0000FF00');
bmi.bV5BlueMask = ostypes.TYPE.DWORD('0x000000FF');
bmi.bV5AlphaMask = ostypes.TYPE.DWORD('0xFF000000'); // 0x00000000 for opaque, otherwise 0xff000000
var cBmi = ctypes.cast(bmi.address(), ostypes.TYPE.BITMAPINFO.ptr); // cBmi is now a pointer so no need to pass cBmi.address() to CreateDIBSection
var pixelBuffer = ostypes.TYPE.BYTE.ptr();
var hbmp = ostypes.API('CreateDIBSection')(hdcMemoryDC, cBmi, ostypes.CONST.DIB_RGB_COLORS, pixelBuffer.address(), null, 0);
This is ctypes so I don't have to memset bmi after creation as by default it is initialized memset 0.
Thanks
I'm writing code with visual c++ using opencv and qt libraries. I'm trying to apply a threshold to an iplImage and displaying it but I've some problems: when I pass my iplImage to cvThreshold function (with an hypothetical threshold of 0) doesn't return a white image and I don't know why.
To display the function I'm using emit:
uchar *qimout=new uchar[sImg];
IplImage *greyImage=cvCreateImage(cvSize(wImg,hImg),IPL_DEPTH_8U,1);
cvThreshold(currentImage,greyImage,0,255,cv::THRESH_BINARY);
greyImage->imageData = (char*)qimout;
emit renderImage(QImage(qimout,wImg,hImg,QImage::Format_Indexed8));
Can someone help me?
Thanks in advance.
I think this is related to this post Convert RGB to Black & White in OpenCV
// C
IplImage *im_rgb = cvLoadImage("image.jpg");
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);
// C++
Mat im_rgb = imread("image.jpg");
Mat im_gray;
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);
// C
IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
cvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
// C++
Mat img_bw = im_gray > 128;
I read image using opencv and save it again, but when I read it later the data not be the same, that I mean after I read the image I save it, then copy the saved image and read the data inside this image but the data will not the same as before, I write small code to do the following
1- read image
2- save the image
3- save image data into text file
4- read the saved image fro step 2
5- compare the values of the image to the values of the text file and print them together
my code is
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <time.h>
#include <stdint.h>
#include "highgui.h"
IplImage *PlainImage=0,*CipherImage=0,*DecPlainImage=0;
void func_printimage()
{
// create a window
cvNamedWindow("Plain Image",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Plain Image", 800, 600);
// show the image
cvShowImage("Plain Image", PlainImage );
// wait for a key
cvNamedWindow("Cipher Image",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Cipher Image", 800, 600);
// show the image
cvShowImage("Cipher Image", CipherImage );
cvSaveImage("CipherImage.jpg",CipherImage,0);
cvWaitKey(0);
}
int main()
{
//i j and k used as counters
int i,j,step,dep,k,ch,L,C,P,sum=0;
uchar *data_byte;
//Define CPU time parameters for each Layer
PlainImage=cvLoadImage("PlainImage.jpg",3);
CipherImage=cvLoadImage("PlainImage.jpg",3);
L = PlainImage->height;
C = PlainImage->width;
P = PlainImage->nChannels;
step = PlainImage->widthStep;
data_byte=CipherImage->imageData;
printf("Image Information are:\nL=%d\n",L);
printf("C=%d\n",C);
printf("P=%d\n",P);
system("pause");
FILE *f1;
f1 = fopen ("cipher1.txt", "wt");
fprintf(f1,"%d\t%d\t%d\t%d\t",L,C,P,CipherImage->depth);
for(k=0;k<L*C*P;k++)
{
fprintf(f1,"%d\t",data_byte[k]);
}
fclose (f1);
func_printimage();
for(k=0;k<L*C*P;k++)
{
data_byte[k]=0;
}
f1 = fopen ("cipher1.txt", "rt");
fscanf (f1,"%d", &L);
fscanf (f1,"%d", &C);
fscanf (f1,"%d", &P);
fscanf (f1,"%d", &dep);
CipherImage=cvLoadImage("CipherImage.jpg",3);
data_byte=CipherImage->imageData;
printf("Image Information are:\nL=%d\n",L);
printf("C=%d\n",C);
printf("P=%d\n",P);
system("pause");
for(k=0;k<L*C*P;k++)
{
fscanf (f1,"%d", &i);
sum+=abs(i-data_byte[k]);
printf("i=%d data=%d\n",i,data_byte[k]);
}
printf("difference=%d\n",sum);
fclose (f1);
system("pause");
return 0;
}
//End of the main Program
jpg images use Lossy Compression.
You should use png images.
Here in this post i will show you that how to Load an image from your chosen directory and then convert it into gray color, and then store the new one(modified) image in a directory C:\Images.
The code is given below:
#include <cv.h>
#include <highgui.h>,
using namespace cv;
int main( )
{
Mat img;
img = imread(“C:\\prado.jpg”, 1 );
if( !img.data )
{
printf( ” No image data \n ” );
return -1;
}
else
prinf(“Your program is working well”);
Mat gray_image;
cvtColor( img, gray_image, CV_RGB2GRAY );
imwrite( “C://images/Gray_Image.jpg”, gray_image);
imshow( “real image”, img);
imshow( “Gray image”, gray_image);
waitKey(0);
return 0;
}
EXPLANATION:
Mat img = imread(“C:\\prado.jpg”, 1 );
this means to get image from my directory and store it in Mat object which is “img” here, actually Mat object store the data of any image.
cvtColor( img, gray_image, CV_RGB2GRAY );
This line convert the originial (RGB) into other color image(GRAY)
imwrite( “C://images/Gray_Image.jpg”, gray_image);
This one store the new modified image which have been store in Mat object “gray_image” in the directory C://images/ you can chose your own directory
I want to convert a UINT16 monochrome image to a 8 bits image, in C++.
I have that image in a
char *buffer;
I'd like to give the new converted buffer to a QImage (Qt).
I'm trying with freeImagePlus
fipImage fimage;
if (fimage.loadfromMemory(...) == false)
//error
loadfromMemory needs a fipMemoryIO adress:
loadfromMemory(fipMemoryIO &memIO, int flag = 0)
So I do
fipImage fimage;
BYTE *buf = (BYTE*)malloc(gimage.GetBufferLength() * sizeof(BYTE));
// 'buf' is empty, I have to fill it with 'buffer' content
// how can I do it?
fipMemoryIO memIO(buf, gimage.GetBufferLength());
fimage.loadFromMemory(memIO);
if (fimage.convertTo8Bits() == true)
cout << "Good";
Then I would do something like
fimage.saveToMemory(...
or
fimage.saveToHandle(...
I don't understand what is a FREE_IMAGE_FORMAT, which is the first argument to any of those two functions. I can't find information of those types in the freeImage documentation.
Then I'd finish with
imageQt = new QImage(destiny, dimX, dimY, QImage::Format_Indexed8);
How can I fill 'buf' with the content of the initial buffer?
And get the data from the fipImage to a uchar* data for a QImage?
Thanks.
The conversion is simple to do in plain old C++, no need for external libraries unless they are significantly faster and you care about such a speedup. Below is how I'd do the conversion, at least as a first cut. The data is converted inside of the input buffer, since the output is smaller than the input.
QImage from16Bit(void * buffer, int width, int height) {
int size = width*height*2; // length of data in buffer, in bytes
quint8 * output = reinterpret_cast<quint8*>(buffer);
const quint16 * input = reinterpret_cast<const quint16*>(buffer);
if (!size) return QImage;
do {
*output++ = *input++ >> 8;
} while (size -= 2);
return QImage(output, width, height, QImage::Format_Indexed8);
}