To get pixel values of images - visual-c++

Below is the code which provides width,height and BGR values of 2 images.But the problem is until i close the first image i cant see the second image.What modifications to be made such that i can see both images at a time and get the all the pixel values.
1 . #include <cv.h>
#include<iostream>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv[])
{
int width,height;
int i=0,j=0,k=3,l=3;
IplImage *img1 = cvLoadImage("E:/images.jpg");
cvNamedWindow("Image1:",1);
cvShowImage("Image1:",img1);
cout << "Width:" << img1->width << endl;
cout << "Height:" << img1->height << endl;
CvScalar s;
s=cvGet2D(img1,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image1:");
IplImage *img2 = cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
cvNamedWindow("Image2:",2);
cvShowImage("Image2:",img2);
cout << "Width:" << img2->width << endl;
cout << "Height:" << img2->height << endl;
s=cvGet2D(img2,k,l); // get the (k,l) pixel value
printf("B1=%f, G1=%f, R1=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image2:");
cvReleaseImage(&img1);
cvReleaseImage(&img2);
return 0;
}

problem is with cvWaitKey()
try:
IplImage *img1 = cvLoadImage("E:/images.jpg");
cvNamedWindow("Image1:",1);
cvShowImage("Image1:",img1);
cout << "Width:" << img1->width << endl;
cout << "Height:" << img1->height << endl;
CvScalar s;
s=cvGet2D(img1,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
IplImage *img2 = cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg");
cvNamedWindow("Image2:",2);
cvShowImage("Image2:",img2);
cout << "Width:" << img2->width << endl;
cout << "Height:" << img2->height << endl;
s=cvGet2D(img2,k,l); // get the (k,l) pixel value
printf("B1=%f, G1=%f, R1=%f\n",s.val[0],s.val[1],s.val[2]);
cvWaitKey();
cvDestroyWindow("Image1:");
cvDestroyWindow("Image2:");
cvReleaseImage(&img1);
cvReleaseImage(&img2);

Related

Numerical Integration with Unspecified Parameter in C++

I have written a simple code to calculate an integral numerically with both the midpoint and trapezoidal rule. It works fine, but now I would like to modify it so that it can calculate the same integral but with an unspecified parameter. So instead of the integral of e^(-x^2), I would like to calculate the integral of e^(-\alpha*x^2). However, I am stuck here. I tried to just declare a new double alpha without initializing it, but that clearly takes me no where. Is there a way to do such a thing in C++? Thank you in advance.
//=============================================================
// A simple program to numerically integrate a Gaussian over
// the interval a to b
//=============================================================
#include <iostream>
#include <cmath>
using namespace std;
// function implementing the midpoint rule
double midpoint(double a, double b, int n){
double sum = 0.0;
double width = (b-a)/n;
for (int i = 1; i < n; i++){
sum += exp((-1)*(a+(i+0.5)*width)*(a+(i+0.5)*width))*width;
}
return sum;
}
//function implementing the trapezoidal rule
double trapezoidal(double a, double b, int n){
double width = (b-a)/n;
double sum = (width/2)*(exp(-a*a)+exp(-b*b));
for (int i = 1; i < n; i++){
sum += 2*exp((-1)*(a+i*width)*(a+i*width))*width/2;
}
return sum;
}
int main(){
cout << "THIS IS A SIMPLE PROGRAM TO INTEGRATE A GAUSSIAN OVER A CERTAIN INTERVAL." << endl;
int n;
double a, b;
cout << "Enter the lower and upper limit of integration as doubles" << endl;
cin >> a >> b;
cout << "Enter the number of partitions" << endl;
cin >> n;
double actual = 0.886207;
double midRule = midpoint(a,b,n);
double trapRule = trapezoidal(a,b,n);
cout << "For the function e^(-x^2) integrated from "<< a << " to " << b << endl;
cout << "The analytic value of the integral is: " << actual << endl;
cout << "The midpoint value of the integral for " << n << " partitions is: " << midRule << endl;
cout << "The trapezoidal value of the integral for " << n << " partitions is: " << trapRule << endl;
cout << "The percent error for the midpoint is: " << (abs(actual-midRule)/actual)*100 << "%" << endl;
cout << "The percent error for the trapezoidal is: " << (abs(actual-trapRule)/actual)*100 << "%" << endl;
return 0;
}

C++ My program is in infinite loop

I appreciate you taking the time to read my problem sorry I'm just a beginner in programming. Apparently I am not that good in loops and if else statement and I don't know whats wrong in my code. when I run it and input "up" its just go to (8,10). it should have been (0,1) then the console will ask which direction they want to go again. Thanks in advance!!! :D
#include<iostream>;
#include<string>;
using namespace std;
int main()
{
int x = 0;
int y = 0;
string input;
cout << "objective: go to (8,9)" << endl;
cout << "current location (" << x << "," << y << ")" << endl;
while ( (x = 8) && (y = 9) )
{
cout << "which way do you want to go?: ";
cin >> input;
if (input == "up")
{
++y;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "down")
{
--y;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "right")
{
++x;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
else if (input == "left")
{
--x;
cout << "current location (" << x << "," << y << ")" << endl;
break;
}
}
cout << "current location (" << x << "," << y << ")" << endl;
cout << "Congratulations! You have reach your destination! :D" << endl;
system("pause");
return(0);
}
"
I tried removing all the break but it went straight down to "congratulations! you have reach your destination" when I tried to run it and I change the condition to (x ==8) && (y ==9) still no improvement.
"
Your loop condition is incorrect. The while loop will only run if x is 8 and y is 9. They are 0 and 0 at the beginning of the program, which explains why we skip the loop. The real condition is: "run the loop as long as x and y are not these values." while (x != 8 || y != 9) and alternately alternately: while (!(x == 8 && y == 9))

Expression must have pointer-to-object type Error?

[Updated Code Pictutre][1]Can someone help me understand what I am doing wrong with my array in this code? It is giving me the same error for all my variables of the array
I dont know how to show you the whole code without adding like 5 pictures, i tried to capture all the important information
Ok I have posted the code here below
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
// output for a single input driving frequency
double m, c1, c2, k1, k2, r, Y;
m = c1 = c2 = k1 = k2 = r = Y = 0;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
double ceq, keq, wn, dampratio, staticamp;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
double cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double Fo, alpha, w, t;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
double Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
double phi = atan((ceq*w) / (keq - m*w*w));
double xp = Xp*sin(w*t - phi);
double H, Td, Ftwall, Ftbase;
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
//*****************************Section 1.2 **********************************************
// 1.2 output for a range of driving frequencies
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = keq = 0;
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m); //natural frequency
dampratio = ceq / (2 * sqrt(m*keq)); // damping ratio
//Determine the Case
cas = 0;
cout << "Which Case (1-5): ";
cin >> cas;
double staticamp[200], Xp[200], xp[200], Fo[200], phi[200], w[200];
double alpha[200], Td[200], H[200], Ftbase[200], Ftwall[200], r[200];
r[1] = .1;
for (int i = 0; i<200; i++) {
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r[i] * wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha[i] = atan(-c2*w / k2);
}
//Static amplitude
staticamp[i] = 0;
staticamp[i] = Fo / keq;
Xp[i] = (Fo / keq) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
phi[i] = atan((ceq*w) / (keq - m*w*w));
xp[i] = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td[i] = 0;
Td[i] = (Fo / (Y*keq)) / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Frequanecy Response
H[i] = 0;
H[i] = 1 / (sqrt(pow((1 - r[i] * r[i]), 2) + pow((2 * dampratio*r[i]), 2)));
//Force Transmissibilty to the bang
Ftbase[i] = 0;
Ftbase[i] = (Xp*sqrt(pow((k1 - keq*r[i] * r[i]), 2) + pow((c1*wn*r[i]), r[i]))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall[i] = 0;
Ftwall[i] = (Xp*sqrt((k1*k1) + pow((c1*r[i] * wn), 2))) / (Y*k2);
//increment r
r[i + 1] = r[i] + .1;
}
ofstream Data(" 1.2 Case 1 .txt"); // File Creation to import in text file to graph
for (int i = 0; i > 200; i++) { //NEED TO CHANGE FILE NAME FOR NEW DATA SET
Data << r[i] << "," << H[i] << "," << phi[i] << "," << Td[i] << "," << Ftbase[i] << "," << Ftwall[i] << endl;
}
//************************* Section 2 ****************
cout << "\t\tSection 2" << endl << endl;
//input values for the case
cout << "Mass: ";
cin >> m;
cout << "c1: ";
cin >> c1;
cout << "c2: ";
cin >> c2;
cout << "k1: ";
cin >> k1;
cout << "k2: ";
cin >> k2;
cout << "r: ";
cin >> r;
cout << "Y: ";
cin >> Y;
//solve for the equivlent values
ceq = c1 + c2;
keq = k1 + k2;
wn = 0;
wn = sqrt(keq / m);
dampratio = ceq / (2 * sqrt(m*keq));
//Determine the Case
cout << "Which Case (1-5): ";
cin >> cas;
if (cas == 2) {
Fo = Y*k2;
}
else {
w = r*wn;
t = 0;
Fo = 0;
Fo = Y*sqrt((k2*k2 + pow((c2*w), 2)));
alpha = atan(-c2*w / k2);
}
//Static amplitude
staticamp = 0;
staticamp = Fo / keq;
Xp = (Fo / keq) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
phi = atan((ceq*w) / (keq - m*w*w));
xp = Xp*sin(w*t - phi);
//Displacement Transmissiblity
Td = 0;
Td = (Fo / (Y*keq)) / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Frequanecy Response
H = 0;
H = 1 / (sqrt(pow((1 - r*r), 2) + pow((2 * dampratio*r), 2)));
//Force Transmissibilty to the bang
Ftbase = 0;
Ftbase = (Xp*sqrt(pow((k1 - keq*r*r), 2) + pow((c1*wn*r), r))) / (Y*k2);
//Force Transmissibilty to the wall
Ftwall = 0;
Ftwall = (Xp*sqrt((k1*k1) + pow((c1*r*wn), 2))) / (Y*k2);
if (cas == 5) {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
}
else {
cout << "\nCase: " << cas << endl << endl;
cout << "Wn: " << wn << endl;
cout << "Damping Ratio: " << dampratio << endl;
cout << "Amplitude: " << staticamp << endl;
cout << "Frequency Response: " << H << endl;
cout << "Phase Angle: " << phi << endl;
cout << "Displacement Transmissibilty: " << Td << endl;
cout << "Force Tranmissibility to the base: " << Ftbase << endl;
cout << "Force Transmissibilty to the wall: " << Ftwall << endl;
}
return 0;
}

How to print out the letters(characters) from a user inputed string?

using an array, i cannot seem to figure out how to print each letter on a separate line from a user inputted string.
Here is the code
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x, z;
string fname, lname, name;
char name_array[20];
cout << "Please enter your name." << endl;
getline(cin, name);
x = name.length();
for(int i = 0; i < x; i++)
cout << name_array[i] << endl << endl;
z = name.find(' ', 0);
fname = name.substr(0, z);
lname = name.substr(z + 1, x);
cout << lname << ", " << fname << endl;
return 0;
}

How do I use cudaMemcpy2D() DeviceToHost

I'm new to cuda and C++ and just can't seem to figure this out.
What I want to do is copy a 2d array A to the device then copy it back to an identical array B.
I would expect that the B array would have the same values as A, but there is something that I'm doing wrong.
CUDA - 4.2, compiling for win32, 64 bit machine, NVIDIA Quadro K5000
Here is the code.
void main(){
cout<<"Host main" << endl;
// Host code
const int width = 3;
const int height = 3;
float* devPtr;
float a[width][height];
//load and display input array
cout << "a array: "<< endl;
for (int i = 0 ; i < width; i ++)
{
for (int j = 0 ; j < height; j ++)
{
a[i][j] = i + j;
cout << a[i][j] << " ";
}
cout << endl;
}
cout<< endl;
//Allocating Device memory for 2D array using pitch
size_t host_orig_pitch = width * sizeof(float); //host original array pitch in bytes
size_t pitch;// pitch for the device array
cudaMallocPitch(&devPtr, &pitch, width * sizeof(float), height);
cout << "host_orig_pitch: " << host_orig_pitch << endl;
cout << "sizeof(float): " << sizeof(float)<< endl;
cout << "width: " << width << endl;
cout << "height: " << height << endl;
cout << "pitch: " << pitch << endl;
cout << endl;
cudaMemcpy2D(devPtr, pitch, a, host_orig_pitch, width, height, cudaMemcpyHostToDevice);
float b[width][height];
//load b and display array
cout << "b array: "<< endl;
for (int i = 0 ; i < width; i ++)
{
for (int j = 0 ; j < height; j ++)
{
b[i][j] = 0;
cout << b[i][j] << " ";
}
cout << endl;
}
cout<< endl;
//MyKernel<<<100, 512>>>(devPtr, pitch, width, height);
//cudaThreadSynchronize();
//cudaMemcpy2d(dst, dPitch,src ,sPitch, width, height, typeOfCopy )
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width, height, cudaMemcpyDeviceToHost);
// should be filled in with the values of array a.
cout << "returned array" << endl;
for(int i = 0 ; i < width ; i++){
for (int j = 0 ; j < height ; j++){
cout<< b[i][j] << " " ;
}
cout<<endl;
}
cout<<endl;
system("pause");
}
Here is the output.
Host main A Array 0 1 2 1 2 3 2 3 4
host_orig_pitch: 12 sizeof(float): 4 width: 3 height: 3 pitch: 512
b array: 0 0 0 0 0 0 0 0 0
returned array 0 0 0
1.17549e-038 0 0 0 0 0
Press any key to continue . . .
If more information is need let me know and I'll post it.
Any help would be greatly appreciated.
As identified in comments, the original poster was supplying incorrect arguments to the cudaMemcpy2Dcall. The width argument for a transfer is always in bytes, so in the above code:
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width, height, cudaMemcpyDeviceToHost);
should be
cudaMemcpy2D(b, host_orig_pitch, devPtr, pitch, width * sizeof(float), height, cudaMemcpyDeviceToHost);
Note this answer was added as a community wiki to get this question off the unanswered list

Resources