Forum Discussion
Hi,
Do you have any further questions?
"I am currently using Python to convert images to text, and in Eclipse, I am able to perform Sobel operations on the text data. I have successfully converted the results back into images using Python for validation. However, I am facing an issue on how to save the Sobel-processed results back to the SD card.
The input image is 100x100, resulting in 10,000 data points. I have tried writing the data back to the SD card from the Nios console, but I can only successfully save approximately 752 records. After that, the data becomes corrupted. How can I overcome this issue?"
"And there are also some errors in the values. For example, the 102nd data point should be 171, but it is recorded as 255."
Nios ii console
txt file
My source code:
#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; 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++) { buffer[j] = alt_up_sd_card_read(sd_fileh); if (buffer[j] == -1 || buffer[j] == '\n') { break; } } if (j == 0) { break; } // Null-terminate the buffer buffer[j] = '\0'; // Print the value read from the file printf("%d\n", atoi(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 file alt_up_sd_card_fclose(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; }