Skip to content

Commit fb82a79

Browse files
authored
Merge pull request #18 from fgervais/broken-pixels
Handle broken pixels
2 parents 085c06b + 5ce481d commit fb82a79

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

adafruit_mlx90640.py

+58-17
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class MLX90640: # pylint: disable=too-many-instance-attributes
9696
cpAlpha = [0] * 2
9797
cpOffset = [0] * 2
9898
ilChessC = [0] * 3
99-
brokenPixels = [0xFFFF] * 5
100-
outlierPixels = [0xFFFF] * 5
99+
brokenPixels = []
100+
outlierPixels = []
101101
cpKta = 0
102102
cpKv = 0
103103

@@ -266,6 +266,11 @@ def _CalculateTo(self, frameData, emissivity, tr, result):
266266
)
267267

268268
for pixelNumber in range(768):
269+
if self._IsPixelBad(pixelNumber):
270+
# print("Fixing broken pixel %d" % pixelNumber)
271+
result[pixelNumber] = -273.15
272+
continue
273+
269274
ilPattern = pixelNumber // 32 - (pixelNumber // 64) * 2
270275
chessPattern = ilPattern ^ (pixelNumber - (pixelNumber // 2) * 2)
271276
conversionPattern = (
@@ -744,30 +749,66 @@ def _ExtractCILCParameters(self):
744749
self.ilChessC = ilChessC
745750

746751
def _ExtractDeviatingPixels(self):
747-
self.brokenPixels = [0xFFFF] * 5
748-
self.outlierPixels = [0xFFFF] * 5
749-
752+
# pylint: disable=too-many-branches
750753
pixCnt = 0
751-
brokenPixCnt = 0
752-
outlierPixCnt = 0
753754

754-
while (pixCnt < 768) and (brokenPixCnt < 5) and (outlierPixCnt < 5):
755+
while (
756+
(pixCnt < 768)
757+
and (len(self.brokenPixels) < 5)
758+
and (len(self.outlierPixels) < 5)
759+
):
755760
if eeData[pixCnt + 64] == 0:
756-
self.brokenPixels[brokenPixCnt] = pixCnt
757-
brokenPixCnt += 1
761+
self.brokenPixels.append(pixCnt)
758762
elif (eeData[pixCnt + 64] & 0x0001) != 0:
759-
self.outlierPixels[outlierPixCnt] = pixCnt
760-
outlierPixCnt += 1
763+
self.outlierPixels.append(pixCnt)
761764
pixCnt += 1
762765

763-
if brokenPixCnt > 4:
766+
if len(self.brokenPixels) > 4:
764767
raise RuntimeError("More than 4 broken pixels")
765-
if outlierPixCnt > 4:
768+
if len(self.outlierPixels) > 4:
766769
raise RuntimeError("More than 4 outlier pixels")
767-
if (brokenPixCnt + outlierPixCnt) > 4:
770+
if (len(self.brokenPixels) + len(self.outlierPixels)) > 4:
768771
raise RuntimeError("More than 4 faulty pixels")
769-
# print("Found %d broken pixels, %d outliers" % (brokenPixCnt, outlierPixCnt))
770-
# TODO INCOMPLETE
772+
# print("Found %d broken pixels, %d outliers"
773+
# % (len(self.brokenPixels), len(self.outlierPixels)))
774+
775+
for brokenPixel1, brokenPixel2 in self._UniqueListPairs(self.brokenPixels):
776+
if self._ArePixelsAdjacent(brokenPixel1, brokenPixel2):
777+
raise RuntimeError("Adjacent broken pixels")
778+
779+
for outlierPixel1, outlierPixel2 in self._UniqueListPairs(self.outlierPixels):
780+
if self._ArePixelsAdjacent(outlierPixel1, outlierPixel2):
781+
raise RuntimeError("Adjacent outlier pixels")
782+
783+
for brokenPixel in self.brokenPixels:
784+
for outlierPixel in self.outlierPixels:
785+
if self._ArePixelsAdjacent(brokenPixel, outlierPixel):
786+
raise RuntimeError("Adjacent broken and outlier pixels")
787+
788+
def _UniqueListPairs(self, inputList):
789+
# pylint: disable=no-self-use
790+
for i, listValue1 in enumerate(inputList):
791+
for listValue2 in inputList[i + 1 :]:
792+
yield (listValue1, listValue2)
793+
794+
def _ArePixelsAdjacent(self, pix1, pix2):
795+
# pylint: disable=no-self-use
796+
pixPosDif = pix1 - pix2
797+
798+
if -34 < pixPosDif < -30:
799+
return True
800+
if -2 < pixPosDif < 2:
801+
return True
802+
if 30 < pixPosDif < 34:
803+
return True
804+
805+
return False
806+
807+
def _IsPixelBad(self, pixel):
808+
if pixel in self.brokenPixels or pixel in self.outlierPixels:
809+
return True
810+
811+
return False
771812

772813
def _I2CWriteWord(self, writeAddress, data):
773814
cmd = bytearray(4)

0 commit comments

Comments
 (0)