CNN methods to convert one-dimensional network traffic that is .csv file into two-dimensional grayscale images

4 views (last 30 days)
Dear Sir/Madam
I need help in converting the one-dimensional network traffic that is .csv file into two-dimensional grayscale images .The CSv file I am using is WSN-DS , i have to convert this file into 2D - grayscale images for categorizing the Normal and other DOS attacks (Black hole, gray hole)in Wireless Sensor Networks.

Answers (1)

Suraj Kumar
Suraj Kumar on 4 Mar 2025
To convert one-dimensional network traffic data from a CSV file into two-dimensional grayscale images using MATLAB, you can refer to the following steps and attached code snippets:
1. Load your CSV data into MATLAB. You can use readtable or csvread depending on the format of your CSV file.
data = csvread('WSN-DS.csv');
2. Preprocess the Data i.e. you might need to normalize or scale the data. This step is crucial to ensure that the values fit into the range required for image representation (usually 0 to 255 for grayscale).
data_min = min(data(:));
data_max = max(data(:));
data_normalized = (data - data_min) / (data_max - data_min) * 255;
3. Convert the one-dimensional data into a two-dimensional matrix. The shape of this matrix will depend on the size of your data and how you plan to visualize it.
data_length = length(data_normalized);
image_size = ceil(sqrt(data_length));
data_padded = padarray(data_normalized, [0, image_size^2 - data_length], 'post');
image_matrix = reshape(data_padded, [image_size, image_size]);
4. Convert the matrix into a grayscale image using uint8 function in MATLAB and then can use the imshow function to display the image.
gray_image = uint8(image_matrix);
imshow(gray_image);
imwrite(gray_image, 'network_traffic_image.png');
Hope this works for you!

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!