Weird second line in QwtPlotCurve - qt

I have a text file where I get my values,
I have a timer which works as an increaser to the x axis so the plot is real time.
here's my code.
void gui::x()
{
int xy = 0;
QVector<double> x(1000), y(1000);
FILE *file;
char line[1024];
file = fopen("x.txt", "r");
while (fgets(line,1024,file) != NULL)
{
fscanf(file,"%lf %lf", &y[xy], &x[xy]);
xy++;
}
fclose(file);
QwtPlotCurve *curve = new QwtPlotCurve;
curve->attach(plot_all[3]);
curve->setData(y,x);
curve->
QwtPlotCurve *curve2 = new QwtPlotCurve;
curve2->attach(plot[3]);
curve2->setData(y,x);
}
the proble is i get a weird second line below my plot.
can anyone help me?

I solved it by using an array instead of a vector.
void gui::ecg()
{
int xy = 0;
double x[1000], y[1000];
FILE *file;
char line[1024];
file = fopen("ecg.txt", "r");
while (fgets(line,1024,file) != NULL)
{
fscanf(file,"%lf %lf", &y[xy], &x[xy]);
xy++;
}
fclose(file);
QwtPlotCurve *curve = new QwtPlotCurve;
curve->attach(plot_all[0]);
curve->setData(x,y,1000);
curve->setPen(QPen(Qt::blue,1));
QwtPlotCurve *curve2 = new QwtPlotCurve;
curve2->setStyle(QwtPlotCurve::Lines);
curve2->attach(plot[0]);
curve2->setData(x,y,1000);
curve2->setPen(QPen(Qt::blue,1));
}

Related

How create an adjacency matrix of a Maze graph

I'm working on making a maze Generator using Prim's Algorithm. I understand i have to make an undirected weighted graph and represent it on an Adjacency Matrix or List. i created the boolean[][] adjacenyMatrix array to show which edges currently exist in the maze. But i have an issue trying to implement the algorithm i thought of. Here is my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter the size of the maze");
int mazeHeight = scanner.nextInt();
int mazeWidth = scanner.nextInt();
int noOfNodes = mazeHeight * mazeWidth;
boolean[][] adjacencyMatrix = new boolean[noOfNodes][noOfNodes];
for (int i = 0; i < mazeHeight; i++) {
for (int j = 0; j < mazeWidth; j++ ) {
// Edges exist from left to right
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
}
}
for (int i = 0; i < mazeWidth; i++) {
for (int j = 0; j < noOfNodes; j + mazeWidth) { // <-----------I'm having an issue here; Not a statement
// Edges exist from top to bottom
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
}
}
}
}
After taking a break; i looked over it and realised that i forgot to include the "=" symbol >.<
so j += mazeWidth

Hashing, can't insert to hash table

struct googlePlayApp{
string name;
string category;
double rating;
int reviews;
googlePlayApp *next;
};
void appInsert(googlePlayApp &newApp, int &cmps) {
int slot = hash1(newApp.name, HASH_SIZE);
int cmps1 = 1;
googlePlayApp *tmp = appHash[slot];
if (tmp == 0)
appHash[slot] = &newApp;
else
{
while(tmp->next != 0)
{
tmp = tmp->next;
cmps1++;
}
tmp->next = &newApp;
}
cmps += cmps1;
}
while (getline(inFile, inputLine)) {
googlePlayApp newApp;
readSingleApp(inputLine, newApp);
appInsert(newApp, cmps);
linesRead++;
}
My program stops on the 65th iteration of the while loop....
64th for the appInsert call...
Why can't I get this to work?
This is a program where it reads a data file and stores it in a hash table and collision dealt with open addressing....
updated question
bool appFind(const string &name, googlePlayApp &foundApp, int &cmps) {
// Implement this function
int slot = hash1(name);
int cmps1 = 1;
googlePlayApp *tmp = appHash[slot];
while(tmp && tmp->name != name)
{
cmps1++;
tmp = tmp->next;
}
cmps += cmps1;
if(tmp)
{
foundApp.name = appHash[slot]->name;
foundApp.category = appHash[slot]->category;
foundApp.rating = appHash[slot]->rating;
foundApp.reviews = appHash[slot]->reviews;
}
else return false;
}
this is my serach function and I'm trying to search if an app exists based on the data I stored from my code above. I'm trying to search it by the hash addresses, but it's not working...

How convert a raw image stored in a byte array to a rgb image with opencv and Java

i am working in the preview of a fingerprint scaner using id3Fingerprint sdk and OpenCV. If i just show the preview from the id3fingerprint sdk all is fine, but if i load it to a Mat object of OpenCV in order to draw some rectangles in the image then:
1.- The fingerprints are displayed in right form but the rectangles are displayed as lines or pixels in random x,y location.
2.- The rectangles are displayed in right form but the fingerprints are displayed "blured" (look the image attached).fingerprints are blured
I think, my problem is when i convert the raw grayscale image (a byte array from the id3fingerprint sdk) to a RGB or RGBA image.
private void showPreview2(FingerImage image){
int height = 750;
int width = 750;
int currentWidth = 0;
int currentHeight = 0;
try {
currentWidth = image.getWidth();
currentHeight = image.getHeight();
} catch (FingerException ex) {
Logger.getLogger(CallingID3Example.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] pixels = image.getPixels();
Mat dest = new Mat();
Mat source = new Mat();
Mat source2 = null;
source2 = new Mat(currentWidth, currentHeight, CvType.CV_8UC1);
source2.put(0, 0, pixels);
MatOfByte pix = new MatOfByte();
Imgcodecs.imencode(".bmp", source2, pix);
source2.put(0, 0, pix.toArray());
Imgproc.cvtColor(source2, source, Imgproc.COLOR_GRAY2RGBA);
try {
int i=0;
for(FingerImage finger : image.getSegments()){
Scalar color;
color = new Scalar(0, 250,0);
FingerBounds bound = image.getSegmentBounds()[i];
Imgproc.rectangle(source, new Point(bound.topLeft.x, bound.topLeft.y), new Point(bound.bottomRight.x, bound.bottomRight.y), color, 3);
double[] pixelTest;
pixelTest = source.get(bound.topLeft.x, bound.topLeft.y);
i++;
}
} catch (FingerException ex) {
Logger.getLogger(CallingID3Example.class.getName()).log(Level.SEVERE, null, ex);
}
gc = canvas.getGraphicsContext2D();
WritableImage writableImage = loadImage(source);
imageView.setImage(writableImage);
}
private WritableImage loadImage(Mat matrix) {
// Encoding the image
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".bmp", matrix, matOfByte);
// Storing the encoded Mat in a byte array
byte[] byteArray = matOfByte.toArray();
// Displaying the image
InputStream in = new ByteArrayInputStream(byteArray);
BufferedImage bufImage = null;
try {
bufImage = ImageIO.read(in);
} catch (IOException ex) {
}
// Creating the Writable Image
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
Thanks for your answer.
You could try something like this:
// You need to know width/height of the image
int width = 0;
int height = 0;
byte[] imageSrc = null;//
// Convert 8bit greyscale byte array to RGBA byte array.
byte[] imageRGBA = new byte[imageSrc.length * 4];
int i;
for (i = 0; i < imageSrc.length; i++) {
imageRGBA[i * 4] = imageRGBA[i * 4 + 1] = imageRGBA[i * 4 + 2] = ((byte) ~imageSrc[i]);
// Invert the source bits
imageRGBA[i * 4 + 3] = -1;// 0xff, that's the alpha.
}
// Convert RGBA byte array to PNG
int samplesPerPixel = 4;
int[] bandOffsets = {0,1,2,3}; // RGBA order
byte[] bgraPixelData = new byte[width * height * samplesPerPixel];
DataBuffer buffer = new DataBufferByte(bgraPixelData, bgraPixelData.length);
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);
ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
System.out.println("image: " + image); // Should print: image: BufferedImage#<hash>: type = 0 ...
ImageIO.write(image, "PNG", new File(path));
Update
To draw rectangle on image:
BufferedImage image = ...
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(x, y, width, height));
graph.dispose();
ImageIO.write(image, "PNG", new File(path));

Read text file saved in SD card line by line

I have data.txt file saved in SD card containing values of variables required for driving 3 stepper motors. I want to read data.txt file line by line, split the line by "," and save each values into respective variables. data.txt file has data something like this:
8.24, 5.67, 7.34
3,86, 3.56, 4.49
5.38, 6.29, 3.67
I want to save value of first integer in x, second in y and third in z and execute the code. Once the code is executed, I want to get next line and load respective values in their respective variables and continue the loop till there is no line left in data.txt file
This code for matrix, let change some type that you need
public Matrix loadIntMapData(InputStream input) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
ArrayList<double[]> list = new ArrayList<double[]>();
while ((line = reader.readLine()) != null) {
if (line.equals("")) {
//ignore
} else {
String[] vs = line.split(",");
double ints[] = new double[vs.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = Double.parseDouble(vs[i]);
}
list.add(ints);
}
}
double[][] map = new double[list.size()][];
for (int i = 0; i < map.length; i++) {
map[i] = list.get(i);
}
return new Matrix(map);
}
public static final void writInteMapData(Matrix matrix, File file) throws IOException {
double[][] map = matrix.getArray();
StringBuffer output = new StringBuffer();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
output.append(Double.toString(map[i][j]));
if (j != map[i].length - 1) {
output.append(",");
}
}
if (i != map.length - 1) {
output.append("\r\n");
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(output.toString());
writer.close();
}

LWJGL executable jar starts then crashes

I would love some help with this. I developed this program for class that reads in four .obj files and one file for a texture. It works fine in my IDE (Eclipse) but when i export it and use JavaSplice to create the final executable jar, it launches, shows a display window for a fraction of a second, then just crashes. With some testing and careful commenting, I've determined that it is not on the splicing end but rather my code. With commenting, I've determined that the problem lies in the Display Lists that read in the four .obj:
int objectOneDisplayList = glGenLists(1);
glNewList(objectOneDisplayList, GL_COMPILE);
{
Model m = null;
try
{
m = OBJLoader.loadModel(new File("res/circle.obj"));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
Display.destroy();
}
catch(IOException e)
{
e.printStackTrace();
Display.destroy();
}
glBegin(GL_TRIANGLES);
for(Face face : m.faces)
{
glColor3f(0.0f, 0.75f, 0.0f);
Vector3f n1 = m.normals.get((int) face.normal.x - 1);
glNormal3f(n1.x, n1.y, n1.z);
Vector3f v1 = m.vertices.get((int) face.vertex.x - 1);
glVertex3f(v1.x, v1.y, v1.z);
Vector3f n2 = m.normals.get((int) face.normal.y - 1);
glNormal3f(n2.x, n2.y, n2.z);
Vector3f v2 = m.vertices.get((int) face.vertex.y - 1);
glVertex3f(v2.x, v2.y, v2.z);
Vector3f n3 = m.normals.get((int) face.normal.z - 1);
glNormal3f(n3.x, n3.y, n3.z);
Vector3f v3 = m.vertices.get((int) face.vertex.z - 1);
glVertex3f(v3.x, v3.y, v3.z);
}
glEnd();
}
glEndList();
There are four of those just with different filenames and list names. Now here is my OBJLoader Class:
package base;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.lwjgl.util.vector.Vector3f;
public class OBJLoader
{
public static Model loadModel(File f) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new FileReader(f));
Model m = new Model();
String line;
while((line = reader.readLine()) != null)
{
if(line.startsWith("v "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.vertices.add(new Vector3f(x, y, z));
}
else if(line.startsWith("vn "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.normals.add(new Vector3f(x, y, z));
}
else if(line.startsWith("f "))
{
Vector3f vertexIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
Float.valueOf(line.split(" ")[2].split("/")[0]),
Float.valueOf(line.split(" ")[3].split("/")[0]));
Vector3f normalIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
Float.valueOf(line.split(" ")[2].split("/")[2]),
Float.valueOf(line.split(" ")[3].split("/")[2]));
m.faces.add(new Face(vertexIndices, normalIndices));
}
}
reader.close();
return m;
}
}
I think it is because I need to use InputFileStream. Could someone show me how I would rewrite this using that?
EDIT: Rewrote the OBJLoader to not use FileReader, still didn't work. I think I can't use any sort of Reader. How can I read the lines then?:
public class OBJLoader
{
public static Model loadModel(File f) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
Model m = new Model();
String line;
while((line = reader.readLine()) != null)
{
if(line.startsWith("v "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.vertices.add(new Vector3f(x, y, z));
}
else if(line.startsWith("vn "))
{
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
m.normals.add(new Vector3f(x, y, z));
}
else if(line.startsWith("f "))
{
Vector3f vertexIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
Float.valueOf(line.split(" ")[2].split("/")[0]),
Float.valueOf(line.split(" ")[3].split("/")[0]));
Vector3f normalIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
Float.valueOf(line.split(" ")[2].split("/")[2]),
Float.valueOf(line.split(" ")[3].split("/")[2]));
m.faces.add(new Face(vertexIndices, normalIndices));
}
}
reader.close();
return m;
}
}
Open command prompt, type:
java -jar "pathToYourJar.jar"
With this you can see what Exception the code throws.
About the crash, i think the problem can be resolved using InputStreams instead of Files.
Change:
BufferedReader reader = new BufferedReader(new InputStreamReader
(new FileInputStream(f)));
into
String path = "/package1/package2/file.obj";
InputStream source = ClassLoader.class.getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader
(source);
This will work both in Eclipse and with JARs.

Resources