Forum Discussion
Hi,
I need some time to check into those codes, anyway where did you get this code? Is it your own? Or have you work with this code working previously?
We do have an old example "edge_detection", but I am still need some time to find the example:
- CK11432 years ago
New Contributor
Hi,
I first conducted practical tests on SD card read/write functionality using official Altera documentation(.pdf). Following that, I incorporated the Sobel filter and proceeded to modify the code.
I may have errors in the code I provided earlier, and I will make corrections.
This time, I will attach the results of the Sobel operation. I used Python to convert the text file to PNG for observation. Since I haven't been able to successfully write back to the SD card yet, I manually copied the results from the Nios II Console.
Original:
I have included the code for writing back to the SD card.
result image:
no writing back to the SD card.
result image:
It's intriguing that after adding the code to write back to the SD card, the Sobel results differ. Removing that part seems to restore the original Sobel outcomes. Currently, I am unable to resolve the issue of not being able to write all Sobel results to the SD card at once, managing to successfully write back only 769 sets of data.
The Sobel results seem to be relatively consistent, with no apparent differences.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <altera_up_sd_card_avalon_interface.h> #define WIDTH 100 #define HEIGHT 100 // 3x3 Sobel operators for gradient calculation int sobel_x[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; int sobel_y[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; void applySobel(short int* imageData, unsigned char* resultData) { memset(resultData, 0, WIDTH * HEIGHT); for (int y = 1; y < HEIGHT - 1; y++) { for (int x = 1; x < WIDTH - 1; x++) { int gx = 0, gy = 0; // Convolution with Sobel operators for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int pixelValue = imageData[(y + i) * WIDTH + (x + j)]; gx += sobel_x[i + 1][j + 1] * pixelValue; gy += sobel_y[i + 1][j + 1] * pixelValue; } } // Calculate gradient magnitude int magnitude = (int)sqrt((double)(gx * gx + gy * gy)); // Clamp magnitude to 0-255 magnitude = (magnitude > 255) ? 255 : ((magnitude < 0) ? 0 : magnitude); resultData[y * WIDTH + x] = (unsigned char)magnitude; } } } int main() { short int sd_fileh,sd_fileh2; alt_up_sd_card_dev *sd_card_dev = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (sd_card_dev != 0 && alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { sd_fileh = alt_up_sd_card_fopen("test8.txt", false); if (sd_fileh >= 0) { short int imageData[WIDTH * HEIGHT]; unsigned char resultData[WIDTH * HEIGHT]; printf("Reading data from test8.txt:\n"); // Read image data into the array for (int i = 0; i < WIDTH * HEIGHT; i++) { char buffer[10]; int j; for (j = 0; j < 4; j++) { // 根據你的像素值調整大小 char currentChar = alt_up_sd_card_read(sd_fileh); if (currentChar == -1 || currentChar == '\n') { break; // 到達檔案結尾或換行字元 } buffer[j] = currentChar; } if (j == 0) { break; } buffer[j] = '\0'; printf("%s\n", buffer); imageData[i] = atoi(buffer); } // Apply Sobel operator to the image data applySobel(imageData, resultData); // Print the Sobel operation result printf("Sobel operation result:\n"); for (int i = 0; i < WIDTH * HEIGHT; i++) { printf("%d\n", resultData[i]); } // Close the original file alt_up_sd_card_fclose(sd_fileh); // // Create and write the result to test11.txt // sd_fileh2 = alt_up_sd_card_fopen("test11.txt", true); // if (sd_fileh2 >= 0) { // for (int i = 0; i < WIDTH * HEIGHT; i++) { // char buffer[10]; // sprintf(buffer, "%d\n", resultData[i]); // alt_up_sd_card_write(sd_fileh2, buffer); // } // alt_up_sd_card_fclose(sd_fileh2); // printf("Sobel result written to test11.txt\n"); // } else { // printf("Problem opening or writing to file. Error %i\n", sd_fileh); // } // // // Close the original file // alt_up_sd_card_fclose(sd_fileh2); } else { printf("Problem opening file. Error %i\n", sd_fileh); } } else { printf("SD card not available or not in FAT16 format\n"); } return 0; }- CK11432 years ago
New Contributor
I have identified the issue with the different results in Sobel. I found that the lines of code 'char buffer[10];' and 'sprintf(buffer, "%d\n", resultData[i]);' are the main culprits.
I have made modifications using an alternative approach. However, there is still a major issue at hand.
How can I write the results of Sobel back to the SD card? I've noticed on the Intel official website that on December 4, 2014, someone inquired about the issue of "SD card HAL driver cannot write large files correctly." When attempting to open files larger than approximately 4K, an error message appears stating, "The file or directory .... is corrupted and unreadable. Please run the Chkdsk utility."
So, currently, I am unable to resolve the issue of saving Sobel results as a text file back to the SD card.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <altera_up_sd_card_avalon_interface.h> #define WIDTH 100 #define HEIGHT 100 // 3x3 Sobel operators for gradient calculation int sobel_x[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; int sobel_y[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; void applySobel(short int* imageData, unsigned char* resultData) { memset(resultData, 0, WIDTH * HEIGHT); for (int y = 1; y < HEIGHT - 1; y++) { for (int x = 1; x < WIDTH - 1; x++) { int gx = 0, gy = 0; // Convolution with Sobel operators for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int pixelValue = imageData[(y + i) * WIDTH + (x + j)]; gx += sobel_x[i + 1][j + 1] * pixelValue; gy += sobel_y[i + 1][j + 1] * pixelValue; } } // Calculate gradient magnitude int magnitude = (int)sqrt((double)(gx * gx + gy * gy)); // Clamp magnitude to 0-255 magnitude = (magnitude > 255) ? 255 : ((magnitude < 0) ? 0 : magnitude); resultData[y * WIDTH + x] = (unsigned char)magnitude; } } } int main() { short int sd_fileh,sd_fileh2; alt_up_sd_card_dev *sd_card_dev = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (sd_card_dev != 0 && alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { sd_fileh = alt_up_sd_card_fopen("test8.txt", false); if (sd_fileh >= 0) { short int imageData[WIDTH * HEIGHT]; unsigned char resultData[WIDTH * HEIGHT]; printf("Reading data from test8.txt:\n"); // Read image data into the array for (int i = 0; i < WIDTH * HEIGHT; i++) { char buffer[10]; int j; for (j = 0; j < 4; j++) { // 根據你的像素值調整大小 char currentChar = alt_up_sd_card_read(sd_fileh); if (currentChar == -1 || currentChar == '\n') { break; // 到達檔案結尾或換行字元 } buffer[j] = currentChar; } if (j == 0) { break; } buffer[j] = '\0'; printf("%s\n", buffer); imageData[i] = atoi(buffer); } // Apply Sobel operator to the image data applySobel(imageData, resultData); // Print the Sobel operation result printf("Sobel operation result:\n"); for (int i = 0; i < WIDTH * HEIGHT; i++) { printf("%d\n", resultData[i]); } // Close the original file alt_up_sd_card_fclose(sd_fileh); // Create and write the result to test11.txt sd_fileh2 = alt_up_sd_card_fopen("test11.txt", true); if (sd_fileh2 >= 0) { for (int i = 0; i < WIDTH * HEIGHT; i++) { // Directly write the resultData[i] to the file with a newline character alt_up_sd_card_write(sd_fileh2, resultData[i]); alt_up_sd_card_write(sd_fileh2, '\n'); } // Close the new file after writing is complete alt_up_sd_card_fclose(sd_fileh2); printf("Sobel result written to test11.txt\n"); } else { printf("Problem opening or writing to file. Error %i\n", sd_fileh); } } else { printf("Problem opening file. Error %i\n", sd_fileh); } } else { printf("SD card not available or not in FAT16 format\n"); } return 0; }