Dont get imagedepth value in C using libtiff - c

I am trying to parse a stack of tiff files using the Library libtiff5. To do so, I first need to read the the imagelength, width and depth. But for the imagedepth, I dont get any value back. The variable a in my code (below) was used to check if the function returns any value but it doesn't.
So my question is: how I can correctly read the imagedepth parameter.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include "tiffio.h"
#include "tiff.h"
int main()
{
TIFF* tif = TIFFOpen("spots.tif", "r");
int w, h, d;
int a;
printf("%d\n", d);
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
a = TIFFGetField(tif, TIFFTAG_IMAGEDEPTH, &d);
printf("width = %d, length = %d, depth = %d\n",w, h, a );
TIFFClose(tif);
return 0;
}

Related

Why is my dataset empty? - error C4700: The uninitialized local variable "coordinates" was used

The data in the txt-file are just two columns with numbers, no labels (x-y coordinates).
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct dataset{
float x;
float y;
} dataset;
int main(){
dataset* coordinates;
FILE* input;
input = fopen("data.txt", "r");
int i = 0;
while (fscanf(input, "%e %e", &coordinates[i].x, &coordinates[i].y) == 2)
i++;
fclose(input);
return 0;
}
Thank you for helping me out.

I am using this code to print from text file but the program gives me "-1.#IND00"

I have a problem. I am using this code to print from text file but the program gives me a different number -such as 11732408.000000- each time. However I don't get this problem when ex is integer.
#include <stdio.h>
#include <string.h>
int main() {
char example[] ="123.12/456 ";
double ex = atof(strtok(example, "/"));
printf("%lf", ex);
return 0;
}
I could solve my problem. Thank you for your helps.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char example[20] ="123.12/456 ";
double ex=atof(strtok(example,"/"));
printf("%lf",ex);
return 0;
}
You forgot to include <stdlib.h> which contains the declaration of atof().
Your compiler is lenient and accepts your code is spite of the missing declaration, and it incorrectly infers the prototype to be int atof(char *), which causes undefined behavior when storing the return value to ex.
Hence the bogus output.
Note also that the l in the format %lf is necessary for scanf() but ignored by printf() as float arguments are implicitly converted to double when passed to vararg functions.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char example[] = "123.12/456 ";
char *p = strtok(example, "/");
if (p != NULL) {
double ex = atof(p);
printf("%f\n", ex);
}
return 0;
}

a.at<uchar>(x,y) wont works in Vivado SDSoC

I want to get 2d array from a Mat in Vivado SDSoC but Im not able to do that because as described Xilinx(XAPP1167),
cv::Mat<>.at() method and cvGet2D() function have no corresponding equivalent
function in the synthesizable library
I appreciate any help. Thank you.
The project is about face recognition system. The system first will go viola-jone face detection and then feed the output to the CNN classification.
The output of viola-jones face detection is in unsigned char*.
I plant to convert it back to Mat and obtain 2d array for the input of CNN Classifier.
#include <cstdio>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <ctime>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sys/mman.h>
#include <malloc.h>
#include <unistd.h>
#include <sds_lib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/core/core_c.h"
using namespace cv;
using namespace std;
void resize_2_gray(unsigned char *imageIn, unsigned char *imageOut)
{
int k=0;
int coord;
for (int i=0; i<240; i++) {
for(int j=0; j<320; j++){
//coord=(i*2*640+j*2)*3;
#pragma HLS PIPELINE II=1
coord=6*(i*640+j);
imageOut[k] = 0.2126*imageIn[coord] + 0.7152*imageIn[coord+1] + 0.0722*imageIn[coord+2] ;
k++;
}
}
}
int main(){
Mat ROI;
unsigned int cam_num =5;//camera USB port
unsigned char *image_ROI;
image_ROI = (unsigned char*)sds_alloc(sizeof(unsigned char)*56*46);
VideoCapture stream(cam_num);
stream.read(ROI);
cvtColor(ROI,ROI, CV_BGR2RGB);
resize_2_gray(ROI.data, image_ROI);
double face_2darray [56][46]={0};
int h1=3, w1=3,w2=46,h2=56;
int x_ratio = (int)((w1<<16)/w2)+1;
int y_ratio = (int)((h1<<16)/h2)+1;
int x2,y2;
//resize image before fed into classification
for (int a=0;a<h2;a++)
{
for (int b=0;b<w2;b++)
{
x2=((b*x_ratio)>>16);
y2=((a*y_ratio)>>16);
image_ROI[(a*w2)+b]=ROI.data[(y2*w1)+x2];
face_2darray[a][b]=(double)image_ROI[a+b];
face_2darray[a][b]= 2*(face_2darray[a][b]/255)-1;
}
}
Mat Image = Mat(56,46,0,image_ROI);
imshow("Output",Image);
sds_free(image_ROI);
}
I believe the synthesized version is using a streaming interface, so you can't use random access APIs, and instead must read pixels/elements one by one.

character array to floating point conversion

I am trying to convert the output buffer(character array)
of the code below to floating point format for further calculations.
Can anybody tell me how to do it.
#include "usbtmc.h"
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <pthread.h>
int main()
{
int myfile;
char buffer[4000];
int actual;
myfile=open("/dev/usbtmc1",O_RDWR);
if(myfile>0)
{
system("echo MEAS:VOLT:AC?>/dev/usbtmc1");
actual=read(myfile,buffer,4000);
buffer[actual] = 0;
printf("Response = \n %s\n",buffer);
close(myfile);
}
return 0;
}
The sample output for this code is
Response =
+1.29273072E-04
You may have two ways:
using double atof(const char* str)
float f;
f = (float)atof(buffer);
printf("%f",f); // here you can use f
using int sscanf( const char * s, const char * format, ...)
float f;
sscanf(buffer,"%f",&f);
printf("%f",f); // here you can use f

GCC division truncates (rounding problem)

Using GCC on the Ubuntu Linux 10.04, I have unwanted rounding after a division.
I tried:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FormatReading(int temp)
{
double reading = temp / 100;
printf("%f\n",reading); /* displays 226.000000, was expecting 226.60 */
}
int main(void)
{
FormatReading(22660);
return 0;
}
It was suggested to me to try:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FormatReading(int temp)
{
long reading = temp ;
reading = reading / 100;
printf("%3.2ld\n",reading); /* displays 226 */
}
int main(void)
{
FormatReading(22660);
return 0;
}
I also tried:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FormatReading(int temp)
{
long reading = temp ;
double reading2 = reading / 100;
printf("%3.2f\n",reading2); /* displays 226.00 */
}
int main(void)
{
FormatReading(22660);
return 0;
}
I also tried the round function using include math.h with compiler tag -lm in various ways, but did not find what I was looking for.
Any help greatly appreciated.
Best regards,
Bert
double reading = temp / 100.0;
^^
temp / 100 is an integer division - that you assign the result to a double doesn't change this.
You are using integer division which always gives integral results rather than fractions, and then the result is being assigned to a double. Divide by 100.0 instead of 100 to get the behavior you want.

Resources