Skip to content

Commit

Permalink
Merge pull request #128 from fdarvas/master
Browse files Browse the repository at this point in the history
updated tests to pass with slight differences for warp matrix and cropping caused by different cv2 versions
  • Loading branch information
poynting committed Nov 6, 2020
2 parents 2bbdf94 + bb937cc commit 8190748
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
14 changes: 11 additions & 3 deletions micasense/image.py
Expand Up @@ -214,7 +214,15 @@ def raw(self):
print("Could not open image at path {}".format(self.path))
raise
return self.__raw_image


def set_raw(self,img):
''' set raw image from input img'''
self.__raw_image = img.astype(np.uint16)

def set_undistorted(self,img):
''' set undistorted image from input img'''
self.__undistorted_image = img.astype(np.uint16)

def set_external_rig_relatives(self,external_rig_relatives):
self.rig_translations = external_rig_relatives['rig_translations']
#external rig relatives are in rad
Expand Down Expand Up @@ -453,9 +461,9 @@ def get_homography(self,ref,R=None,T=None):
R = rotations_degrees_to_rotation_matrix(self.rig_relatives)
if T is None:
T =np.zeros(3)

R_ref = rotations_degrees_to_rotation_matrix(ref.rig_relatives)
A = np.zeros((4,4))
A[0:3,0:3]=R
A[0:3,0:3]=np.dot(R_ref.T,R)
A[0:3,3]=T
A[3,3]=1.
C, _ = cv2.getOptimalNewCameraMatrix(self.cv2_camera_matrix(),
Expand Down
8 changes: 5 additions & 3 deletions micasense/panel.py
Expand Up @@ -223,8 +223,10 @@ def region_stats(self, img, region, sat_threshold=None):
mean_value = panel_pixels.mean()
saturated_count = 0
if sat_threshold is not None:
saturated_px = np.asarray(np.where(panel_pixels > sat_threshold))
saturated_count = saturated_px.sum()
saturated_count = (panel_pixels > sat_threshold).sum()
#set saturated pixels here
if num_pixels>0:
self.saturated_panel_pixels_pct = (100.0*saturated_count)/num_pixels
return mean_value, stdev, num_pixels, saturated_count

def raw(self):
Expand Down Expand Up @@ -278,4 +280,4 @@ def plot(self, figsize=(14,14)):
ax.imshow(display_img)
plt.tight_layout()
plt.show()
return fig, ax
return fig, ax
3 changes: 2 additions & 1 deletion tests/conftest.py
Expand Up @@ -94,6 +94,7 @@ def non_panel_altum_capture(non_panel_altum_file_list):
def panel_image_name():
image_path = os.path.join('data', '0000SET', '000')
return os.path.join(image_path, 'IMG_0000_1.tif')


@pytest.fixture()
def panel_image_name_red():
Expand Down Expand Up @@ -175,4 +176,4 @@ def panel_10band_rededge_capture(panel_10band_rededge_file_list):

@pytest.fixture()
def flight_10band_rededge_capture(flight_10band_rededge_file_list):
return capture.Capture.from_filelist(flight_10band_rededge_file_list)
return capture.Capture.from_filelist(flight_10band_rededge_file_list)
7 changes: 7 additions & 0 deletions tests/test_capture.py
Expand Up @@ -25,6 +25,7 @@

import pytest
import os, glob
import numpy as np

import micasense.capture as capture
import micasense.image as image
Expand Down Expand Up @@ -278,3 +279,9 @@ def test_10_band_irradiance(flight_10band_rededge_capture):
test_irradiance = flight_10band_rededge_capture.dls_irradiance()
good_irradiance = [0.67305, 0.62855, 0.55658, 0.34257, 0.41591, 0.57470, 0.64203, 0.53739, 0.48215, 0.44563]
assert test_irradiance == pytest.approx(good_irradiance, abs = 1e-5)

def test_get_warp_matrices(panel_altum_capture):
for i in range(len(panel_altum_capture.images)):
w = panel_altum_capture.get_warp_matrices(i)
np.testing.assert_allclose(np.eye(3),w[i],atol=1e-6)

4 changes: 2 additions & 2 deletions tests/test_imageutils.py
Expand Up @@ -85,9 +85,9 @@ def test_image_properties(non_panel_altum_capture):
def test_warp_matrices(non_panel_altum_capture):
warp_matrices = non_panel_altum_capture.get_warp_matrices()
for index,warp_matrix in enumerate(warp_matrices):
assert(warp_matrix == pytest.approx(truth_warp_matrices[index]))
assert(warp_matrix == pytest.approx(truth_warp_matrices[index],rel=1e-2))

def test_cropping(non_panel_altum_capture):
warp_matrices = non_panel_altum_capture.get_warp_matrices()
cropped_dimensions,_ = imageutils.find_crop_bounds(non_panel_altum_capture,warp_matrices)
assert(cropped_dimensions == expected_dimensions)
assert(cropped_dimensions == pytest.approx(expected_dimensions,abs=1))
29 changes: 28 additions & 1 deletion tests/test_panel.py
Expand Up @@ -28,6 +28,7 @@
import math
import micasense.image as image
import micasense.panel as panel
import operator

def test_qr_corners(panel_image_name):
img = image.Image(panel_image_name)
Expand All @@ -50,6 +51,10 @@ def test_panel_corners(panel_image_name):
assert panel_pts is not None
assert len(panel_pts) == len(good_pts)
assert pan.serial == 'RP02-1603036-SC'
# the particular order of the points is not relevant
# so sort by coordinates
panel_pts = sorted(panel_pts,key=operator.itemgetter(0,1))
good_pts = sorted(good_pts,key=operator.itemgetter(0,1))
for i, pt in enumerate(panel_pts):
# different opencv/zbar versions round differently it seems
assert pt[0] == pytest.approx(good_pts[i][0], abs=3)
Expand All @@ -75,6 +80,23 @@ def test_raw_panel_manual(panel_image_name):
assert num == pytest.approx(26005, rel=0.001)
assert sat == pytest.approx(0, abs=2)

# test saturated pixels with modified panel picture
def test_raw_panel_saturatedl(panel_image_name):
img = image.Image(panel_image_name)
pan = panel.Panel(img,panelCorners=[[809, 613], [648, 615], [646, 454], [808, 452]])

#saturate 2500 pixels in the raw image - note that on the undistorted image this
#will result in 2329 saturated pixels
i0 = img.undistorted(img.raw())
i0[500:550,700:750]=4095*16+1
img.set_undistorted(i0)

mean, std, num, sat = pan.raw()
assert mean == pytest.approx(47245, rel=0.01)
assert std == pytest.approx(5846.1, rel=0.05)
assert num == pytest.approx(26005, rel=0.001)
assert sat == pytest.approx(2500, abs=0)

def test_raw_panel(panel_image_name):
img = image.Image(panel_image_name)
pan = panel.Panel(img)
Expand Down Expand Up @@ -128,7 +150,12 @@ def test_altum_panel(altum_panel_image_name):
assert panel_pts is not None
assert len(panel_pts) == len(good_pts)
assert pan.serial == 'RP04-1901231-SC'
print(panel_pts)

# the particular order of the points is not relevant
# so sort by coordinates
panel_pts = sorted(panel_pts,key=operator.itemgetter(0,1))
good_pts = sorted(good_pts,key=operator.itemgetter(0,1))

for i, pt in enumerate(panel_pts):
# different opencv/zbar versions round differently it seems
assert pt[0] == pytest.approx(good_pts[i][0], abs=3)
Expand Down

0 comments on commit 8190748

Please sign in to comment.