yosri ben salah Posted June 7, 2017 Report Share Posted June 7, 2017 hi ! i write this code that compare two pixel value of images. i need help to debug this Code thank you #include "systemC.h" #include "string.h" #include "stdio.h" #include"stdlib.h" #include <time.h> #include <fstream> #include <iostream> #include <fstream> using namespace std; #define _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_WARNINGS double elapsed; int K = 0; int H = 0; int in = false; int ret = 0; clock_t start = clock(); SC_MODULE(imageProcess) { sc_in<bool>sig; SC_CTOR(imageProcess) { SC_METHOD(MotionDetector) } void MotionDetector() { printf("\n Running Automation"); char *mode1 = "r"; char *mode2 = "w"; int i, j, k; int C = 0; int rows1, cols1, rows2, cols2; bool fileFound = false; FILE *image1; FILE *image2; FILE *image3; int sum = 0; image3 = fopen("F:/image3.txt", mode2); do { char *mode1 = "r"; char *mode2 = "w"; image1 = fopen("F:/image1.txt", mode1); if (!image1) { printf("File Not Found!!\n"); fileFound = true; } else fileFound = false; } while (fileFound); do { image2 = fopen("F:/image2.txt", mode1); if (!image2) { printf("File Not Found!!\n"); fileFound = true; } else fileFound = false; } while (fileFound); rows1 = rows2 = 384; cols1 = cols2 = 512; int **mat1 = (int **)malloc(rows1 * sizeof(int*)); for (i = 0; i < rows1; i++) mat1 = (int *)malloc(cols1 * sizeof(int)); i = 0; int **mat2 = (int **)malloc(rows2 * sizeof(int*)); for (i = 0; i < rows2; i++) mat2 = (int *)malloc(cols2 * sizeof(int)); i = 0; while (!feof(image1)) { for (i = 0; i < rows1; i++) { for (j = 0; j < cols1; j++) fscanf(image1, "%d%", &mat1[j]); } } i = 0; j = 0; while (!feof(image2)) { for (i = 0; i < rows2; i++) { for (j = 0; j < cols2; j++) fscanf(image2, "%d%", &mat2[j]); } } i = 0; j = 0; printf("\n\n"); for (i = 0; i < rows1; i++) { for (j = 0; j < cols1; j++) { if (mat1[j] != mat2[j]) C++; } } i = j = 0; if ((C/196608) > 0.95) { printf("%d,&K"); printf("MOTION...DETECTED"); getchar(); sc_pause; for (i = 0; i < rows1; i++) { for (j = 0; j < cols1; j++) { fprintf(image3, "%d ", mat2[j]); } fprintf(image3, "\n"); } printf("\n Image Saved...."); std::ofstream mon_fichier("F:\toto.txt"); mon_fichier << elapsed << '\n'; } fclose(image1); fclose(image2); fclose(image3); clock_t end = clock(); elapsed = ((double)end - start) / CLOCKS_PER_SEC; printf("time is %f", elapsed); } }; int sc_main(int argc, char* argv[]) { imageProcess master("EE2"); master.MotionDetector(); sc_start(); return(0); } Quote Link to comment Share on other sites More sharing options...
apfitch Posted June 11, 2017 Report Share Posted June 11, 2017 Hi Yosri, I don't understand why you want to do this in SystemC, the algorithm you've written just reads from a file and writes to a file, which is well suited to a plain C or C++ program. Some specific comments - you have declared "sig" and haven't used it. In sc_main you've called the SC_METHOD using master.MotionDetector. You don't need to do that, the SC_METHOD is a parallel thread and will run automatically at time 0. However as I said above, my main comment is why are you using SystemC? Why not just write a plain C/C++ program? regards Alan Quote Link to comment Share on other sites More sharing options...
yosri ben salah Posted June 11, 2017 Author Report Share Posted June 11, 2017 Hi dr, i really wanna to thank you for your response, im demanded to write systemC that compare two images for my project graduation if there any suggestion dr. the code above is my attempt. the program should infinitely verify if a 1 number is written in a text file the code should compare the two images and print result, (sorry for my bad english) any help dr thank you Quote Link to comment Share on other sites More sharing options...
yosri ben salah Posted June 15, 2017 Author Report Share Posted June 15, 2017 Quote Link to comment Share on other sites More sharing options...
yosri ben salah Posted June 15, 2017 Author Report Share Posted June 15, 2017 6 minutes ago, yosri ben salah said: pls any explanation why i dont get any result for this code. i want that function"synchronization" read the text file then it enables the second function to be executed SC_MODULE(ImageProcessing) { sc_signal<bool> signal; SC_CTOR(ImageProcessing) { SC_THREAD(MotionDetector) sensitive << signal; } void synchronisation() { _CRT_SECURE_NO_WARNINGS cout << "\n Running Automation"; SwitchContext = fopen("F:\SWITCH CONTEXT.txt", mode1); while (fscanf(SwitchContext, "%d", &L) != 1) { cout << "waiting..."; cout << "", &L; //wait(); SC_PAUSED; //getchar(); } signal = true; cout << "Establishing communication"; } void MotionDetector() { printf("\nCommuncation established"); char *mode1 = "r"; char *mode2 = "w"; int i, j, k; int C = 0; int rows1, cols1, rows2, cols2; bool fileFound = false; bool multiplcation = true; FILE *image1; FILE *image2; FILE *image3; int sum = 0; image3 = fopen("F:/image3.txt", mode2); do { char *mode1 = "r"; char *mode2 = "w"; image1 = fopen("F:/image1.txt", mode1); if (!image1) { printf("File Not Found!!\n"); fileFound = true; } else fileFound = false; } while (fileFound); do { image2 = fopen("F:/image2.txt", mode1); if (!image2) { cout << "File Not Found!!\n"; fileFound = true; } else fileFound = false; } while (fileFound); //allocate Matrcies rows1 = rows2 = 384; cols1 = cols2 = 512; int **mat1 = (int **)malloc(rows1 * sizeof(int*)); for (i = 0; i < rows1; i++) mat1 = (int *)malloc(cols1 * sizeof(int)); i = 0; int **mat2 = (int **)malloc(rows2 * sizeof(int*)); for (i = 0; i < rows2; i++) mat2 = (int *)malloc(cols2 * sizeof(int)); i = 0; while (!feof(image1)) { for (i = 0; i < rows1; i++) { for (j = 0; j < cols1; j++) fscanf(image1, "%d%", &mat1[j]); } } i = 0; j = 0; while (!feof(image2)) { for (i = 0; i < rows2; i++) { for (j = 0; j < cols2; j++) fscanf(image2, "%d%", &mat2[j]); } } i = 0; j = 0; printf("\n\n"); i = 0; k = 0; cout << "\n\n"; for (i = 0; i < rows1; i++) { for (j = 0; j < cols1; j++) { a = abs(mat1[j] - mat2[j]); b = b + a; } } i = j = 0; D = b / 196608; if (D > 0.9) { printf("MOTION...DETECTED\a \a"); for (i = 0; i < rows1; i++) { for (j = 0; j < cols1; j++) { fprintf(image3, "%d ", mat2[j]); } fprintf(image3, "\n"); } cout << "\n Image Saved...."; } fclose(image1); fclose(image2); fclose(image3); } }; int sc_main(int argc, char* argv[]) { getchar(); sc_start(); return(0); } Quote Link to comment Share on other sites More sharing options...
maehne Posted June 21, 2017 Report Share Posted June 21, 2017 There are many issues in your code: You do not instantiate your module ImageProcessing in sc_main(). The member function ImageProcessing::synchronisation() is never used. From the code, you probably want that it shall be executed as a SC_THREAD. You mix C and C++ for memory allocation and IO. I suggest you to use <iostream> instead of <stdio.h> for your input and output needs. Use new and delete instead of malloc() and free() if you really need it. However, it is usually better to use a suitable container, e.g., std::vector instead of raw pointers and manually allocated memory. Prefer to #include <systemc> over <systemc.h>, <cstdlib> over <stdlib.h>, etc. to avoid polluting the public namespace. The variable b, used in your code, doesn't seem to have been declared. If b is an int, the division by 196608 won't yield the floating point result, which you expect as an integer division will be performed. ... These are just the ones, which jump to the eye. It seems you are not only struggling with SystemC, but also with C++. I suggest you to read a good introductory book on C++ (Stroustrup: "Programming: Principles and Practice Using C++" or "The C++ Programming Language") as well as on SystemC (e.g. Black, Donovan et al.: "SystemC from the Ground Up"). yosri ben salah 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.