Skip to content

Commit 5f21a07

Browse files
author
LAKESIDE\LindaT18
committed
fix roi plot orientationm spot greyscale
1 parent 0c29d95 commit 5f21a07

File tree

3 files changed

+84
-37
lines changed

3 files changed

+84
-37
lines changed

PythonGUI_apps/Image_analysis/Image_analysis.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# local modules
1111

1212
pg.mkQApp()
13-
pg.setConfigOption('imageAxisOrder', 'row-major')
13+
pg.setConfigOption('imageAxisOrder', 'col-major')
1414

1515
base_path = Path(__file__).parent
1616
file_path = (base_path / "image_analysis_gui.ui").resolve()
@@ -36,11 +36,13 @@ def __init__(self):
3636
self.imv = pg.ImageView()
3737
self.imv.getView().setAspectLocked(lock=False, ratio=1)
3838
self.imv.getView().setMouseEnabled(x=True, y=True)
39-
#self.imv.getView().invertY(False)
39+
self.imv.getView().invertY(False)
4040
self.roi = self.imv.roi
4141

4242
self.roi.translateSnap = True
4343
self.roi.scaleSnap = True
44+
self.roi.removeHandle(1)
45+
self.roi.addScaleHandle([0, 0], [1, 1])
4446
self.update_camera() #initialize camera pixel size
4547
self.update_scaling_factor() #initialize scaling_factor
4648

@@ -53,17 +55,19 @@ def __init__(self):
5355
self.ui.custom_pixel_size_checkBox.stateChanged.connect(self.switch_custom_pixel_size)
5456
self.ui.update_scaling_factor_pushButton.clicked.connect(self.reload_image)
5557
self.ui.spot_radioButton.toggled.connect(self.update_camera)
58+
self.ui.custom_pixel_size_spinBox.valueChanged.connect(self.update_scaling_factor)
5659

5760
self.show()
5861

62+
#row major. invert y false, rotate false
5963
def load_image(self):
6064
"""
6165
Prompts the user to select a text file containing image data.
6266
"""
6367
try:
6468
file = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', os.getcwd())
6569
self.original_image = Image.open(file[0])
66-
#self.original_image = self.original_image.rotate(-90, expand=True) #correct image orientation
70+
self.original_image = self.original_image.rotate(-90, expand=True) #correct image orientation
6771
self.resize_to_scaling_factor(self.original_image)
6872
except Exception as err:
6973
print(format(err))
@@ -72,8 +76,6 @@ def resize_to_scaling_factor(self, image):
7276
"""
7377
Handles loading of image according to scaling_factor
7478
"""
75-
self.update_camera() #initialize camera pixel size
76-
self.update_scaling_factor() #initialize scaling_factor
7779

7880
if self.ui.pixera_radioButton.isChecked():
7981
image = self.original_image
@@ -86,18 +88,15 @@ def resize_to_scaling_factor(self, image):
8688
image_array = np.array(image)
8789
width = image_array.shape[0]
8890
height = image_array.shape[1]
89-
if self.ui.pixera_radioButton.isChecked():
90-
width = width * self.scaling_factor
91-
height = height * self.scaling_factor
9291

9392
try:
9493
x_vals = np.arange(width)
94+
if self.ui.pixera_radioButton.isChecked():
95+
x_vals = x_vals * self.scaling_factor
9596
self.imv.setImage(image_array, xvals= x_vals)
96-
9797
roi_height = self.scaling_factor * height
98-
self.roi.setPos((0,height - roi_height))
98+
self.roi.setPos((0, 0))
9999
self.roi.setSize([width, roi_height])
100-
101100
scale = pg.ScaleBar(size=1,suffix='um')
102101
scale.setParentItem(self.imv.view)
103102
scale.anchor((1, 1), (1, 1), offset=(-30, -30))
@@ -117,27 +116,29 @@ def line_profile_update_plot(self):
117116
if data is None:
118117
return
119118

120-
x_values = coords[1][0]
121-
122-
#calculate sums along columns in region
119+
x_values = coords[0,:,0]
120+
if self.ui.pixera_radioButton.isChecked():
121+
x_values = x_values * self.scaling_factor
122+
123+
#calculate average along columns in region
123124
if len(data.shape) == 2: #if grayscale, average intensities
124-
sums_to_plot = np.mean(data, axis=0)
125+
avg_to_plot = np.mean(data, axis=-1)
125126
try:
126-
self.roi_plot.plot(x_values, sums_to_plot)
127+
self.roi_plot.plot(x_values, avg_to_plot)
127128
except:
128129
pass
129130
elif len(data.shape) > 2: #if rgb arrays, plot individual components
130131
r_values = data[:,:,0]
131132
g_values = data[:,:,1]
132133
b_values = data[:,:,2]
133-
r_avg = np.mean(r_values, axis=0) #average red values across columns
134-
g_avg = np.mean(g_values, axis=0) #average green values
135-
b_avg = np.mean(b_values, axis=0) #average blue values
134+
r_avg = np.mean(r_values, axis=-1) #average red values across columns
135+
g_avg = np.mean(g_values, axis=-1) #average green values
136+
b_avg = np.mean(b_values, axis=-1) #average blue values
136137
try:
137138
self.roi_plot.plot(x_values, r_avg, pen='r')
138139
self.roi_plot.plot(x_values, g_avg, pen='g')
139140
self.roi_plot.plot(x_values, b_avg, pen='b')
140-
except:
141+
except Exception as e:
141142
pass
142143

143144
def update_scaling_factor(self):
@@ -164,10 +165,11 @@ def update_camera(self):
164165
if self.ui.spot_radioButton.isChecked():
165166
self.camera_pixel_size = 7.4
166167
self.ui.greyscale_checkBox.setChecked(False)
168+
self.update_scaling_factor()
167169
elif self.ui.pixera_radioButton.isChecked():
168170
self.camera_pixel_size = 3
169171
self.ui.greyscale_checkBox.setChecked(True)
170-
172+
self.update_scaling_factor()
171173
def close_application(self):
172174
choice = QtGui.QMessageBox.question(self, 'EXIT!',
173175
"Do you want to exit the app?",

PythonGUI_apps/Image_analysis/image_analysis_gui.ui

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@
5151
</item>
5252
<item row="0" column="0">
5353
<layout class="QGridLayout" name="gridLayout">
54-
<item row="6" column="0">
55-
<widget class="QPushButton" name="load_image_pushButton">
54+
<item row="0" column="0">
55+
<widget class="QCheckBox" name="greyscale_checkBox">
5656
<property name="text">
57-
<string>Load image</string>
57+
<string>Greyscale image</string>
5858
</property>
5959
</widget>
6060
</item>
61-
<item row="0" column="0">
62-
<widget class="QCheckBox" name="greyscale_checkBox">
61+
<item row="7" column="0">
62+
<widget class="QPushButton" name="load_image_pushButton">
6363
<property name="text">
64-
<string>Greyscale image</string>
64+
<string>Load image</string>
6565
</property>
6666
</widget>
6767
</item>
68-
<item row="3" column="1">
68+
<item row="4" column="1">
6969
<widget class="QComboBox" name="magnification_comboBox">
7070
<item>
7171
<property name="text">
@@ -89,27 +89,34 @@
8989
</item>
9090
</widget>
9191
</item>
92-
<item row="3" column="0">
92+
<item row="4" column="0">
9393
<widget class="QLabel" name="label_7">
9494
<property name="text">
9595
<string>Magnification</string>
9696
</property>
9797
</widget>
9898
</item>
99-
<item row="5" column="0">
100-
<widget class="QCheckBox" name="custom_pixel_size_checkBox">
99+
<item row="9" column="0">
100+
<widget class="QPushButton" name="update_scaling_factor_pushButton">
101101
<property name="text">
102-
<string>Custom pixel size (um)</string>
102+
<string>Update settings</string>
103103
</property>
104104
</widget>
105105
</item>
106-
<item row="5" column="1">
106+
<item row="6" column="1">
107107
<widget class="QDoubleSpinBox" name="custom_pixel_size_spinBox">
108108
<property name="enabled">
109109
<bool>false</bool>
110110
</property>
111111
</widget>
112112
</item>
113+
<item row="6" column="0">
114+
<widget class="QCheckBox" name="custom_pixel_size_checkBox">
115+
<property name="text">
116+
<string>Custom pixel size (um)</string>
117+
</property>
118+
</widget>
119+
</item>
113120
<item row="2" column="0" colspan="2">
114121
<widget class="QGroupBox" name="groupBox">
115122
<property name="title">
@@ -136,11 +143,30 @@
136143
</layout>
137144
</widget>
138145
</item>
139-
<item row="8" column="0">
140-
<widget class="QPushButton" name="update_scaling_factor_pushButton">
141-
<property name="text">
142-
<string>Update scaling factor</string>
146+
<item row="3" column="0" colspan="2">
147+
<widget class="QGroupBox" name="groupBox_3">
148+
<property name="title">
149+
<string>Direction to average pixels</string>
143150
</property>
151+
<layout class="QGridLayout" name="gridLayout_5">
152+
<item row="0" column="0">
153+
<widget class="QRadioButton" name="vertical_radioButton">
154+
<property name="text">
155+
<string>Vertical</string>
156+
</property>
157+
<property name="checked">
158+
<bool>true</bool>
159+
</property>
160+
</widget>
161+
</item>
162+
<item row="1" column="0">
163+
<widget class="QRadioButton" name="horizontal_radioButton">
164+
<property name="text">
165+
<string>Horizontal</string>
166+
</property>
167+
</widget>
168+
</item>
169+
</layout>
144170
</widget>
145171
</item>
146172
</layout>

PythonGUI_apps/test_im.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
from pathlib import Path
3+
import os.path
4+
import pyqtgraph as pg
5+
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets#, QColorDialog
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
from PIL import Image
9+
10+
scale = 7.4/50
11+
image = Image.open(r"C:\Users\lindat18\Desktop\BREAD.png")
12+
image = image.rotate(-90, expand=True)
13+
image = image.resize((round(image.size[0]*scale), round(image.size[1]*scale)))
14+
image_array = np.array(image)
15+
width = image_array.shape[0]
16+
height = image_array.shape[1]
17+
print(image_array.shape)
18+
arr = image_array[:,0,0]
19+
print(arr.shape)

0 commit comments

Comments
 (0)