Converting analogue input values to grey values
2 views (last 30 days)
Show older comments
Hi everyone,
I'm currently working on this project: Ultrasound Sonography, where an arduino GLCD screen is used to look at where the boundaries of the skin are e.g.
This is schoieved in arduino by reading in analogue values (voltages) and converting them to gray values (0-255) and drawn one below the other in the form of short horziontal lines with the respective gray values. A strong echo pulse is correspoding to a bright white line. The code available for this is shown below:
#define pin_output 5
#define pin_input_magnification 4
#define pin_input_sleep 3
unsigned long start_time;
unsigned long stop_time;
unsigned int values[600]; // Array mit den eingelesenen Spannungswerten
int i, j;
int Trigger_time; // Dauer des Triggerpulses
int gray_value;
#define CENTRE 240
#include <UTFT.h>
// Declare which fonts we will be using
extern uint8_t SmallFont[];
UTFT myGLCD (ILI9486,38,39,40,41);
// =========================
// ========= SETUP =========
// =========================
void setup()
{
Serial.begin(115200);
REG_ADC_MR = 0x10380080; // change from 10380200 to 10380080, 1 is the PREESCALER and 8 means FREERUN
ADC -> ADC_CHER = 0x03; // enable ADC on pin A7
pinMode(pin_output, OUTPUT);
pinMode(pin_input_magnification, INPUT); // Switch-input for magnification
pinMode(pin_input_sleep, INPUT); // Switch-input for sleep-mode
digitalWrite(pin_output, LOW);
Trigger_time = 1; // Dauer des Triggerpulses
// Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
myGLCD.clrScr();
myGLCD.setColor(255, 255, 0);
myGLCD.fillRect(0, 0, 432, 13);
myGLCD.setColor(0, 0, 0);
myGLCD.setBackColor(255, 255, 0);
myGLCD.print("Sonographie - stoppi", CENTER, 1);
}
// ========================
// ========= LOOP =========
// ========================
void loop()
{
for (j = 0; j < 18; j++) // Darstellung von 18 scans nebeneinander in x-Richtung
{
// Ausgabe des Triggersignals
// ==========================
digitalWrite(pin_output,HIGH);
//delayMicroseconds(Trigger_time);
digitalWrite(pin_output,LOW);
// Einlesen der US-Reflexionen
// ===========================
//start_time = micros();
if (digitalRead(pin_input_magnification) == LOW) // ohne Zeitstreckung Einlesen von 300 Werten; erfasste Zeitspanne = 120 µsec
{
for(i = 0; i < 300; i++)
{
while((ADC->ADC_ISR & 0x03)==0); // wait for conversion
values[i] = ADC->ADC_CDR[0]; //get values
//delayMicroseconds(1);
}
}
else // mit Zeitstreckung Einlesen von 600 Werten; erfasste Zeitspanne = 240 µsec
{
for(i = 0; i < 600; i++)
{
while((ADC->ADC_ISR & 0x03)==0); // wait for conversion
values[i] = ADC->ADC_CDR[0]; //get values
//delayMicroseconds(1);
}
}
//delayMicroseconds(100);
//delay(5);
/*
stop_time = micros();
Serial.print("Total time for 300 values: ");
Serial.print(stop_time-start_time);
Serial.println(" microseconds");
Serial.print("Average time in microseconds per conversion: ");
Serial.println((float)(stop_time-start_time)/300);
*/
/*
Serial.println("Values: ");
for(i = 0;i < 600; i++)
{
Serial.println(values[i]);
}
*/
// Zeichnen des scans
// ==================
if (digitalRead(pin_input_magnification) == LOW) // ohne Zeitstreckung Darstellung der ersten 300 eingelesenen Werte
{
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(0, 0, 0);
myGLCD.drawLine(440, 15, 440, 15 + 300);
myGLCD.drawLine(440, 15, 445, 15);
myGLCD.drawLine(440, 65, 445, 65);
myGLCD.drawLine(440, 115, 445, 115);
myGLCD.drawLine(440, 165, 445, 165);
myGLCD.drawLine(440, 215, 445, 215);
myGLCD.drawLine(440, 265, 445, 265);
myGLCD.drawLine(440, 315, 445, 315);
myGLCD.print("0 us", 450, 10);
myGLCD.print("20 ", 450, 60);
myGLCD.print("40 ", 450, 110);
myGLCD.print("60 ", 450, 160);
myGLCD.print("80 ", 450, 210);
myGLCD.print("100", 450, 260);
myGLCD.print("120", 450, 310);
for(i = 0; i < 300; i++)
{
values[i] = map(values[i], 0, 4095, 0, 255); // Bringe die Helligkeitswerte in den Zahlenbereich [0,255]
//values[i] = random(255);
myGLCD.setColor(values[i], values[i], values[i]);
myGLCD.fillRect(j * 24, 15 + i, j * 24 + 23, 15 + i);
}
}
else // mit Zeitstreckung Darstellung nur jedes zweiten Werts der 600 eingelesenen Werte
{
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(0, 0, 0);
myGLCD.drawLine(440, 15, 440, 15 + 300);
myGLCD.drawLine(440, 15, 445, 15);
myGLCD.drawLine(440, 65, 445, 65);
myGLCD.drawLine(440, 115, 445, 115);
myGLCD.drawLine(440, 165, 445, 165);
myGLCD.drawLine(440, 215, 445, 215);
myGLCD.drawLine(440, 265, 445, 265);
myGLCD.drawLine(440, 315, 445, 315);
myGLCD.print("0 us", 450, 10);
myGLCD.print("40 ", 450, 60);
myGLCD.print("80 ", 450, 110);
myGLCD.print("120", 450, 160);
myGLCD.print("160", 450, 210);
myGLCD.print("200", 450, 260);
myGLCD.print("240", 450, 310);
for(i = 0; i < 300; i++)
{
values[2*i] = map(values[2*i], 0, 4095, 0, 255); // Bringe die Helligkeitswerte in den Zahlenbereich [0,255]
//values[2*i] = random(255);
myGLCD.setColor(values[2*i], values[2*i], values[2*i]);
myGLCD.fillRect(j * 24, 15 + i, j * 24 + 23, 15 + i);
}
}
// Pause-Taster abfragen
// =====================
if (digitalRead(pin_input_sleep) == HIGH)
{
while(digitalRead(pin_input_sleep) == HIGH)
{
// sleeping
}
}
delay(1000);
}
}
Now... to my question. I don't have a GLCD screen and was planning on taking the analog values and storing them in an excel sheet. Then taking the values to MATLAB to create the same idea as the code shown below, where the bright lines would show the strong echo pulses. However i've tried some built in fucntions with MATLAB such as:
A = readmatrix('dummyvalues.xlsx');
convertTogray = (255*mat2gray(A));
image(convertTogray)
colormap(gray)
colorbar;
However, the image I get is very mixed with black and white lines and is not very clear, I'm not sure if there is a better method or not.
Thanks in advance.
3 Comments
DGM
on 27 Sep 2021
Edited: DGM
on 27 Sep 2021
With this syntax, image() treats everything as an indexed image. If perhaps you had previously specified a colormap of length ~=255, it will not display correctly. In order to make sure the colormap is always correctly set in cases like this that depend on its length, explicitly specify the map length.
colormap(gray(255));
Alternatively, you can use imagesc() instead of jumping through hoops to make image() work.
A = readmatrix('dummyvalues.xlsx');
imagesc(A)
colormap(gray)
colorbar;
Or you can use imshow() as Yanqi Liu suggested, or you can similarly let it do the normalization internally:
A = readmatrix('dummyvalues.xlsx');
imshow(A,[])
colormap(gray)
colorbar;
If none of that helps, you might want to provide an example image.
Image Analyst
on 27 Sep 2021
@Ashleigh Reid, you forgot to attach 'dummyvalues.xlsx'. Can you do that? Make it easy for people to help you.
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!