Thermal Image Processing - python

I'm trying to stream FLIR Lepton 3.5 with Python OPENCV using "VideoCapture" & "cv2.imshow" script. Then, I'll do some detection and control. Here's the problem I have, I was only able to get a very faint black/gray video stream with what seems to be a couple of rows of dead pixels at the bottom of the stream. This is expected since the output is supposed to be 16-Bit RAW image data. So,
I'm trying to convert to RGB888 image data so the stream has "colors".
Why is the stream video in static mode, it doesn't stream video like normal embedded notebook webcam?
I've tried the codes/scripts that were shared by others and even the example code from FLIR application notes, but didn't work. Your help is appreciated.
Environment: Windows 10, Python 3.7.6, PyCharm, OpenCV (latest), FLIR Lepton 3.5 camera/PureThermal2
Code:
import cv2
import numpy as np
image_counter = 0
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
video.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('Y','1','6',' '))
video.set(cv2.CAP_PROP_CONVERT_RGB, 0)
if video.isOpened(): # try to get the first frame
rval, frame = video.read()
else:
rval = False
while rval:
normed = cv2.normalize(frame, None, 0, 65535, cv2.NORM_MINMAX)
nor=cv2.cvtColor(np.uint8(normed),cv2.COLOR_GRAY2BGR)
cv2.imshow("preview", cv2.resize(nor, dsize= (640, 480), interpolation = cv2.INTER_LINEAR))
key = cv2.waitKey(1)
if key == 27: # exit on ESC
break

It's challenging to give an answer without having the camera.
Please note that I could not verify my solution.
I found the following issues with your code:
rval, frame = video.read() must be inside the while loop.
The code grabs the next frame.
If you want to grab more than one frame, you should execute it in a loop.
normed = cv2.normalize(frame, None, 0, 65535, cv2.NORM_MINMAX)
Returns uint16 values in range [0, 65535].
You are getting an overflow when converting to uint8 by np.uint8(normed).
I recommend normalizing to range [0, 255].
You may also select the type of the result to be uint8:
normed = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
Here is the complete updated code (not tested):
import cv2
import numpy as np
image_counter = 0
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
video.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('Y','1','6',' '))
video.set(cv2.CAP_PROP_CONVERT_RGB, 0)
if video.isOpened(): # try to get the first frame
rval, frame = video.read()
else:
rval = False
# Create an object for executing CLAHE.
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
while rval:
# Get a Region of Interest slice - ignore the last 3 rows.
frame_roi = frame[:-3, :]
# Normalizing frame to range [0, 255], and get the result as type uint8.
normed = cv2.normalize(frame_roi, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# Apply CLAHE - contrast enhancement.
# Note: apply the CLAHE on the uint8 image after normalize.
# CLAHE supposed to work with uint16 - you may try using it without using cv2.normalize
cl1 = clahe.apply(normed)
nor = cv2.cvtColor(cl1, cv2.COLOR_GRAY2BGR) # Convert gray-scale to BGR (no really needed).
cv2.imshow("preview", cv2.resize(nor, dsize=(640, 480), interpolation=cv2.INTER_LINEAR))
key = cv2.waitKey(1)
if key == 27: # exit on ESC
break
# Grab the next frame from the camera.
rval, frame = video.read()
Colorizing:
https://groups.google.com/g/flir-lepton/c/Cm8lGQyspmk
Result:
Here is the code sample with colorizing (using "Iron Black" color map):
import cv2
import numpy as np
# https://groups.google.com/g/flir-lepton/c/Cm8lGQyspmk
def generateColourMap():
"""
Conversion of the colour map from GetThermal to a numpy LUT:
https://github.com/groupgets/GetThermal/blob/bb467924750a686cc3930f7e3a253818b755a2c0/src/dataformatter.cpp#L6
"""
lut = np.zeros((256, 1, 3), dtype=np.uint8)
colormapIronBlack = [
255, 255, 255, 253, 253, 253, 251, 251, 251, 249, 249, 249, 247, 247,
247, 245, 245, 245, 243, 243, 243, 241, 241, 241, 239, 239, 239, 237,
237, 237, 235, 235, 235, 233, 233, 233, 231, 231, 231, 229, 229, 229,
227, 227, 227, 225, 225, 225, 223, 223, 223, 221, 221, 221, 219, 219,
219, 217, 217, 217, 215, 215, 215, 213, 213, 213, 211, 211, 211, 209,
209, 209, 207, 207, 207, 205, 205, 205, 203, 203, 203, 201, 201, 201,
199, 199, 199, 197, 197, 197, 195, 195, 195, 193, 193, 193, 191, 191,
191, 189, 189, 189, 187, 187, 187, 185, 185, 185, 183, 183, 183, 181,
181, 181, 179, 179, 179, 177, 177, 177, 175, 175, 175, 173, 173, 173,
171, 171, 171, 169, 169, 169, 167, 167, 167, 165, 165, 165, 163, 163,
163, 161, 161, 161, 159, 159, 159, 157, 157, 157, 155, 155, 155, 153,
153, 153, 151, 151, 151, 149, 149, 149, 147, 147, 147, 145, 145, 145,
143, 143, 143, 141, 141, 141, 139, 139, 139, 137, 137, 137, 135, 135,
135, 133, 133, 133, 131, 131, 131, 129, 129, 129, 126, 126, 126, 124,
124, 124, 122, 122, 122, 120, 120, 120, 118, 118, 118, 116, 116, 116,
114, 114, 114, 112, 112, 112, 110, 110, 110, 108, 108, 108, 106, 106,
106, 104, 104, 104, 102, 102, 102, 100, 100, 100, 98, 98, 98, 96, 96,
96, 94, 94, 94, 92, 92, 92, 90, 90, 90, 88, 88, 88, 86, 86, 86, 84, 84,
84, 82, 82, 82, 80, 80, 80, 78, 78, 78, 76, 76, 76, 74, 74, 74, 72, 72,
72, 70, 70, 70, 68, 68, 68, 66, 66, 66, 64, 64, 64, 62, 62, 62, 60, 60,
60, 58, 58, 58, 56, 56, 56, 54, 54, 54, 52, 52, 52, 50, 50, 50, 48, 48,
48, 46, 46, 46, 44, 44, 44, 42, 42, 42, 40, 40, 40, 38, 38, 38, 36, 36,
36, 34, 34, 34, 32, 32, 32, 30, 30, 30, 28, 28, 28, 26, 26, 26, 24, 24,
24, 22, 22, 22, 20, 20, 20, 18, 18, 18, 16, 16, 16, 14, 14, 14, 12, 12,
12, 10, 10, 10, 8, 8, 8, 6, 6, 6, 4, 4, 4, 2, 2, 2, 0, 0, 0, 0, 0, 9,
2, 0, 16, 4, 0, 24, 6, 0, 31, 8, 0, 38, 10, 0, 45, 12, 0, 53, 14, 0,
60, 17, 0, 67, 19, 0, 74, 21, 0, 82, 23, 0, 89, 25, 0, 96, 27, 0, 103,
29, 0, 111, 31, 0, 118, 36, 0, 120, 41, 0, 121, 46, 0, 122, 51, 0, 123,
56, 0, 124, 61, 0, 125, 66, 0, 126, 71, 0, 127, 76, 1, 128, 81, 1, 129,
86, 1, 130, 91, 1, 131, 96, 1, 132, 101, 1, 133, 106, 1, 134, 111, 1,
135, 116, 1, 136, 121, 1, 136, 125, 2, 137, 130, 2, 137, 135, 3, 137,
139, 3, 138, 144, 3, 138, 149, 4, 138, 153, 4, 139, 158, 5, 139, 163,
5, 139, 167, 5, 140, 172, 6, 140, 177, 6, 140, 181, 7, 141, 186, 7,
141, 189, 10, 137, 191, 13, 132, 194, 16, 127, 196, 19, 121, 198, 22,
116, 200, 25, 111, 203, 28, 106, 205, 31, 101, 207, 34, 95, 209, 37,
90, 212, 40, 85, 214, 43, 80, 216, 46, 75, 218, 49, 69, 221, 52, 64,
223, 55, 59, 224, 57, 49, 225, 60, 47, 226, 64, 44, 227, 67, 42, 228,
71, 39, 229, 74, 37, 230, 78, 34, 231, 81, 32, 231, 85, 29, 232, 88,
27, 233, 92, 24, 234, 95, 22, 235, 99, 19, 236, 102, 17, 237, 106, 14,
238, 109, 12, 239, 112, 12, 240, 116, 12, 240, 119, 12, 241, 123, 12,
241, 127, 12, 242, 130, 12, 242, 134, 12, 243, 138, 12, 243, 141, 13,
244, 145, 13, 244, 149, 13, 245, 152, 13, 245, 156, 13, 246, 160, 13,
246, 163, 13, 247, 167, 13, 247, 171, 13, 248, 175, 14, 248, 178, 15,
249, 182, 16, 249, 185, 18, 250, 189, 19, 250, 192, 20, 251, 196, 21,
251, 199, 22, 252, 203, 23, 252, 206, 24, 253, 210, 25, 253, 213, 27,
254, 217, 28, 254, 220, 29, 255, 224, 30, 255, 227, 39, 255, 229, 53,
255, 231, 67, 255, 233, 81, 255, 234, 95, 255, 236, 109, 255, 238, 123,
255, 240, 137, 255, 242, 151, 255, 244, 165, 255, 246, 179, 255, 248,
193, 255, 249, 207, 255, 251, 221, 255, 253, 235, 255, 255, 24]
def colormapChunk(ulist, step):
return map(lambda i: ulist[i: i + step], range(0, len(ulist), step))
chunks = colormapChunk(colormapIronBlack, 3)
red = []
green = []
blue = []
for chunk in chunks:
red.append(chunk[0])
green.append(chunk[1])
blue.append(chunk[2])
lut[:, 0, 0] = blue
lut[:, 0, 1] = green
lut[:, 0, 2] = red
return lut
# Generate color map - used for colorizing the video frame.
colorMap = generateColourMap()
image_counter = 0
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
video.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('Y','1','6',' '))
video.set(cv2.CAP_PROP_CONVERT_RGB, 0)
if video.isOpened(): # try to get the first frame
rval, frame = video.read()
else:
rval = False
# Create an object for executing CLAHE.
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
while rval:
# Get a Region of Interest slice - ignore the last 3 rows.
frame_roi = frame[:-3, :]
# Normalizing frame to range [0, 255], and get the result as type uint8.
normed = cv2.normalize(frame_roi, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# Apply CLAHE - contrast enhancement.
# Note: apply the CLAHE on the uint8 image after normalize.
# CLAHE supposed to work with uint16 - you may try using it without using cv2.normalize
cl1 = clahe.apply(normed)
nor = cv2.cvtColor(cl1, cv2.COLOR_GRAY2BGR) # Convert gray-scale to BGR (no really needed).
colorized_img = cv2.LUT(nor, colorMap) # Colorize the gray image with "false colors".
cv2.imshow("preview", cv2.resize(colorized_img, dsize=(640, 480), interpolation=cv2.INTER_LINEAR))
key = cv2.waitKey(1)
if key == 27: # exit on ESC
break
# Grab the next frame from the camera.
rval, frame = video.read()
Note:
The IR sensor is not a colored sensor.
Colorizing the frames uses "false colors" - colorizing may used for eustatic purposes.
The "false colors" has no physical meaning.
There are many ways to colorize an IR image, and there is no "standard colorization" method.

For colorizing you could try radiometric_image=cv2.applyColorMap(radiometric_image,cv2.COLORMAP_JET)
Previously having a temperature data, modified to normalized data by radiometric_image=cam_source.raw_to_8bit(thermal_frame)
It works for me on Lepton 3.5 on Nano

Related

converting decimal to actual bytes

I have a task which is to extract and analyse a payload. the payload is stored in the array below:
byte[] source = new byte[449]
{
252, 72, 131, 228, 240, 232, 204,
0, 0, 0, 65, 81, 65, 80,
82, 81, 72, 49, 210, 86,
101, 72, 139, 82, 96, 72, 139,
82, 24, 72, 139, 82, 32,
72, 139, 114, 80, 77, 49, 201, 72,
15, 183, 74, 74, 72, 49, 192, 172,
60, 97, 124, 2, 44, 32,
65, 193, 201, 13, 65, 1, 193, 226,
237, 82, 72, 139,82, 32, 139, 66,
60, 72, 1, 208, 102, 129, 120, 24, 11,
2,65, 81, 15, 133, 114, 0, 0, 0,
139, 128, 136, 0, 0, 0, 72, 133,
192, 116, 103, 72, 1, 208, 80, 139,
72, 24, 68, 139, 64, 32, 73, 1, 208,
227, 86, 72, 255, 201, 65, 139, 52, 136,
72, 1, 214, 77, 49, 201, 72, 49,
192, 172, 65, 193, 201, 13, 65, 1,
193, 56, 224, 117, 241, 76, 3, 76,
36, 8, 69, 57, 209, 117, 216, 88, 68,
139, 64, 36, 73, 1, 208, 102, 65, 139,
12, 72, 68, 139, 64, 28, 73, 1, 208,
65, 139, 4, 136, 65, 88, 65, 88,
72, 1, 208, 94, 89, 90, 65, 88, 65,
89, 65, 90, 72, 131, 236, 32, 65,
82, 255, 224, 88, 65, 89, 90, 72, 139, 18,
233, 75, 255, 255, 255, 93, 73,
190, 119, 115, 50, 95, 51, 50, 0, 0,
65, 86, 73, 137, 230, 72, 129,
236, 160, 1, 0, 0, 73, 137, 229, 73,
188, 2, 0, 1, 187, 51, 161, 134, 90,
65, 84, 73, 137, 228, 76, 137, 241,
65, 186, 76, 119, 38, 7, 255, 213, 76,
137, 234, 104, 1, 1, 0, 0, 89,
65, 186, 41, 128, 107, 0, 255, 213, 106, 10,
65, 94, 80, 80, 77, 49, 201,77, 49, 192, 72,
255, 192, 72, 137, 194,
72, 255, 192, 72, 137, 193, 65,
186, 234, 15, 223, 224, 255, 213, 72, 137,
199, 106, 16, 65, 88, 76, 137, 226, 72,
137, 249, 65, 186, 153, 165, 116, 97,
255, 213, 133, 192, 116, 12, 73, 255,
206, 117, 229, 104, 240, 181, 162, 86,
255, 213, 72, 131, 236, 16, 72, 137,
226, 77, 49, 201, 106, 4, 65, 88,
72, 137, 249, 65, 186, 2, 217, 200, 95,
255, 213, 72, 131, 196, 32, 94, 137,
246, 106, 64, 65, 89, 104, 0,
16, 0, 0, 65, 88, 72, 137, 242,
72, 49, 201, 65, 186, 88, 164, 83,
229, 255, 213, 72, 137, 195, 73, 137,
199, 77, 49, 201, 73, 137, 240,
72, 137, 218, 72, 137, 249, 65, 186, 2,
217, 200, 95, 255, 213, 72,
1, 195, 72, 41, 198, 72, 133, 246, 117,
225, 65, 255, 231,
};
can anyone help with this? like how to convert it e.g writing a python script or if there are any online converters (I looked but I couldn't find anything).
I need it in actual bytes format so I can analyse it and understand what the payload does exactly.
note - I do not mean the data type byte
thanks
This is an unusual question, as you're already creating a Java bytes object, but if you want a Python bytes object, you can use bytes.
From the documentation:
bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object
Construct an immutable array of bytes from:
- an iterable yielding integers in range(256)
- a text string encoded using the specified encoding
- any object implementing the buffer API.
- an integer
In this case, we're interested in bytes(iterable_of_ints), which we pass a Python list of your arguments.
source=[
252, 72, 131, 228, 240, 232, 204,
0, 0, 0, 65, 81, 65, 80,
82, 81, 72, 49, 210, 86,
101, 72, 139, 82, 96, 72, 139,
82, 24, 72, 139, 82, 32,
72, 139, 114, 80, 77, 49, 201, 72,
15, 183, 74, 74, 72, 49, 192, 172,
60, 97, 124, 2, 44, 32,
65, 193, 201, 13, 65, 1, 193, 226,
237, 82, 72, 139,82, 32, 139, 66,
60, 72, 1, 208, 102, 129, 120, 24, 11,
2,65, 81, 15, 133, 114, 0, 0, 0,
139, 128, 136, 0, 0, 0, 72, 133,
192, 116, 103, 72, 1, 208, 80, 139,
72, 24, 68, 139, 64, 32, 73, 1, 208,
227, 86, 72, 255, 201, 65, 139, 52, 136,
72, 1, 214, 77, 49, 201, 72, 49,
192, 172, 65, 193, 201, 13, 65, 1,
193, 56, 224, 117, 241, 76, 3, 76,
36, 8, 69, 57, 209, 117, 216, 88, 68,
139, 64, 36, 73, 1, 208, 102, 65, 139,
12, 72, 68, 139, 64, 28, 73, 1, 208,
65, 139, 4, 136, 65, 88, 65, 88,
72, 1, 208, 94, 89, 90, 65, 88, 65,
89, 65, 90, 72, 131, 236, 32, 65,
82, 255, 224, 88, 65, 89, 90, 72, 139, 18,
233, 75, 255, 255, 255, 93, 73,
190, 119, 115, 50, 95, 51, 50, 0, 0,
65, 86, 73, 137, 230, 72, 129,
236, 160, 1, 0, 0, 73, 137, 229, 73,
188, 2, 0, 1, 187, 51, 161, 134, 90,
65, 84, 73, 137, 228, 76, 137, 241,
65, 186, 76, 119, 38, 7, 255, 213, 76,
137, 234, 104, 1, 1, 0, 0, 89,
65, 186, 41, 128, 107, 0, 255, 213, 106, 10,
65, 94, 80, 80, 77, 49, 201,77, 49, 192, 72,
255, 192, 72, 137, 194,
72, 255, 192, 72, 137, 193, 65,
186, 234, 15, 223, 224, 255, 213, 72, 137,
199, 106, 16, 65, 88, 76, 137, 226, 72,
137, 249, 65, 186, 153, 165, 116, 97,
255, 213, 133, 192, 116, 12, 73, 255,
206, 117, 229, 104, 240, 181, 162, 86,
255, 213, 72, 131, 236, 16, 72, 137,
226, 77, 49, 201, 106, 4, 65, 88,
72, 137, 249, 65, 186, 2, 217, 200, 95,
255, 213, 72, 131, 196, 32, 94, 137,
246, 106, 64, 65, 89, 104, 0,
16, 0, 0, 65, 88, 72, 137, 242,
72, 49, 201, 65, 186, 88, 164, 83,
229, 255, 213, 72, 137, 195, 73, 137,
199, 77, 49, 201, 73, 137, 240,
72, 137, 218, 72, 137, 249, 65, 186, 2,
217, 200, 95, 255, 213, 72,
1, 195, 72, 41, 198, 72, 133, 246, 117,
225, 65, 255, 231,
]
my_bytes=bytes(source)
print(my_bytes)
```
Presumably you want to take a number represented by a decimal string and convert in to a sting of 8 0's and 1's (i.e. have leading 0's)
The way to do this is
a='100'
format(int(a),'0=8b')
'01100100'
If you want to apply it to every string in a list you can use map
a=['0','1','2']
list(map(lambda k: format(int(k),'0=8b'),a))
['00000000', '00000001', '00000010']

Python : Creating raw ECC-ECDSA-SECP256R1 private(32-bytes) and public(64-bytes) keys

I want to generate raw 32-bytes private key and raw 64-bytes public key (ECC-SECP256R1) using Python.
Which library should I install in my Python 3.7 and what APIs do I call to be able to generate raw keys?
Please help.
TIA.
The ECDSA library can do this.
Generate a key pair:
import ecdsa
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.get_verifying_key()
# Your byte values will vary, each time you generate(). Here is an example pair.
skBytes = sk.to_string()
print(f"Signing key = {[b for b in skBytes]}")
# [242, 122, 66, 46, 189, 185, 103, 40, 162, 156, 155, 213, 77, 38, 251, 23, 148, 207, 49, 206, 122, 63, 117, 150, 196, 115, 82, 203, 95, 104, 60, 227]
vkBytes = vk.to_string()
print(f"Verifying key = {[b for b in vkBytes]}")
# [31, 164, 220, 214, 106, 22, 45, 112, 244, 49, 124, 250, 217, 209, 51, 83, 161, 239, 137, 124, 25, 220, 112, 116, 80, 9, 64, 130, 9, 27, 92, 198, 123, 146, 213, 154, 142, 148, 90, 6, 142, 177, 31, 151, 215, 9, 216, 36, 51, 27, 222, 224, 132, 7, 39, 227, 48, 31, 244, 218, 206, 59, 249, 196]
The key pair can be restored from bytes:
skBytes = bytes([242, 122, 66, 46, 189, 185, 103, 40, 162, 156, 155, 213, 77, 38, 251, 23, 148, 207, 49, 206, 122, 63, 117, 150, 196, 115, 82, 203, 95, 104, 60, 227])
vkBytes = bytes([31, 164, 220, 214, 106, 22, 45, 112, 244, 49, 124, 250, 217, 209, 51, 83, 161, 239, 137, 124, 25, 220, 112, 116, 80, 9, 64, 130, 9, 27, 92, 198, 123, 146, 213, 154, 142, 148, 90, 6, 142, 177, 31, 151, 215, 9, 216, 36, 51, 27, 222, 224, 132, 7, 39, 227, 48, 31, 244, 218, 206, 59, 249, 196])
sk = ecdsa.SigningKey.from_string(skBytes, curve=ecdsa.SECP256k1)
vk = ecdsa.VerifyingKey.from_string(vkBytes, curve=ecdsa.SECP256k1)
# or vk = sk.get_verifying_key()
Example signing and verifying:
message = b"Hello, world!"
signature = sk.sign(message)
print(f"{[b for b in signature]}")
# [161, 22, 110, 48, 232, 36, 152, 153, 22, 7, 177, 219, 157, 102, 237, 57, 243, 216, 186, 207, 22, 168, 170, 247, 216, 235, 160, 13, 35, 37, 141, 237, 63, 50, 84, 31, 203, 95, 212, 91, 13, 150, 156, 125, 255, 197, 30, 133, 193, 174, 129, 174, 192, 33, 90, 160, 243, 78, 96, 92, 38, 1, 237, 129]
isSignatureValid = vk.verify(signature, message)
print(f"{isSignatureValid=}")
# isSignatureValid=True
Credit to this question for introducing me to the ecdsa package:
ECDSA Signing and Verifying issue between python ECDSA and C micro-ecc library

Change ALL gray scale image pixel with white if gray value is in the list without a list

I have a gray scale image and a list of gray scale value as defined below:
grayImg= cv2.imread(file,0)
grayList = [102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115]
Now I want to change ALL pixels of the grayImg to "white or 255" if the gray scale value belongs to my grayList without a loop. How can I do this in a NumPyish way?
This will change all the pixels of grayImg that are in grayList to 255. Can't think of a shorter way to do it.
grayImg[np.isin(grayImg, grayList)] = 255
array([[255, 87, 52, ..., 245, 2, 236],
[ 20, 255, 255, ..., 33, 205, 15],
[255, 81, 255, ..., 17, 255, 255],
...,
[236, 255, 255, ..., 217, 15, 255],
[255, 221, 39, ..., 88, 240, 46],
[ 17, 219, 224, ..., 255, 255, 204]])
Use in1d:
grayImg= np.array([0,3,5,102,106,4,56,107])
grayList = np.array([102,103,104,105,106,107,108,109,110,111,112,113,114,115])
grayImg[np.in1d(grayImg, grayList)] = 255
Ouptut grayImg:
array([ 0, 3, 5, 255, 255, 4, 56, 255])
Here is another way, using a Lookup Table, or LUT. It is just an 8-bit, 256-element array of values in which you look-up the current pixel values to find the new value.
My LUT looks like this - hopefully you can see your values in the range 102..115 map to 255:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 116,
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246,
247, 248, 249, 250, 251, 252, 253, 254, 255], dtype=uint8)
I did some timing to compare it against np.isin() and it comes out 3-8x faster depending on the size of the input array, specifically 3 times faster for input array 64x64 and 8 times faster for 640x640:
#!/usr/bin/env python3
import cv2
import numpy as np
def useLUT(im, grayList):
"""Make LUT and apply to image"""
LUT = np.arange(256, dtype=np.uint8) # Straight no-op LUT 0..255
LUT[grayList] = 255 # Values in list get changed to 255
res = cv2.LUT(im, LUT) # Apply LUT
return res
def useIsin(im, grayList):
"""Make pixels in grayList into white"""
im[np.isin(im, grayList)] = 255
return im
# Load image as greyscale
im = cv2.imread('image640.png',0)
# Your graylist
grayList = [102,103,104,105,106,107,108,109,110,111,112,113,114,115]
# Time and compare results
%timeit resA = useIsin(im, grayList)
%timeit resB = useLUT(im, grayList)
Result
13.4 ms ± 122 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1.5 ms ± 95.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
I applied it to this input image:
Here is the result:
grayImg[np.where((gray<min(grayList)) & (gray>max(grayList)))] = 255
should work if you mean a continious interval.

convert finger template data to image

I have finger template data of size 498. I want to convert it to an image. the template does not have any size, the length of template data is 498 bytes.This is the template data.
i tried using this, 'Image.frombuffer('L', (498,1), ba, 'raw', 'L', 0, 1)' but gives only single white line in black background
[4, 41, 80, 3, 41, 130, 134, 225, 196, 184, 63, 255, 137, 50, 182, 95, 3, 86, 250, 7, 218, 114, 246, 93, 132, 101, 56, 224, 233, 234, 185, 149, 132, 245, 245, 239, 1, 171, 245, 214, 66, 198, 133, 254, 18, 19, 247, 92, 72, 151, 118, 152, 21, 131, 248, 128, 74, 103, 172, 72, 37, 163, 233, 146, 196, 54, 244, 15, 85, 91, 229, 89, 195, 22, 64, 200, 73, 131, 246, 215, 133, 166, 137, 237, 85, 27, 246, 85, 68, 70, 122, 208, 134, 179, 246, 77, 199, 119, 246, 184, 145, 147, 247, 73, 143, 217, 240, 152, 149, 3, 56, 8, 183, 58, 13, 10, 181, 131, 248, 29, 80, 8, 1, 1, 217, 131, 248, 32, 11, 248, 250, 232, 245, 99, 245, 83, 196, 22, 192, 183, 21, 156, 246, 60, 74, 104, 106, 209, 14, 212, 233, 155, 195, 248, 247, 231, 21, 236, 245, 66, 136, 87, 102, 201, 33, 92, 233, 149, 1, 200, 185, 224, 50, 100, 248, 31, 8, 184, 120, 7, 62, 20, 247, 52, 9, 136, 108, 201, 62, 4, 248, 33, 11, 185, 116, 239, 105, 156, 246, 179, 78, 40, 203, 45, 121, 204, 248, 156, 196, 117, 77, 215, 138, 20, 247, 175, 77, 57, 203, 221, 149, 156, 213, 40, 28, 186, 108, 36, 182, 252, 248, 145, 7, 185, 139, 200, 210, 180, 248, 142, 199, 167, 139, 200, 209, 252, 233, 140, 70, 248, 197, 208, 233, 12, 246, 155, 140, 105, 151, 168, 9, 213, 246, 143, 8, 152, 215, 200, 26, 29, 233, 130, 132, 249, 71, 233, 38, 37, 248, 134, 7, 199, 5, 217, 73, 69, 248, 128, 197, 214, 3, 225, 133, 93, 247, 126, 131, 215, 1, 225, 158, 237, 247, 4, 65, 7, 194, 39, 73, 28, 117, 86, 133, 198, 255, 127, 121, 165, 117, 121, 194, 24, 126, 250, 161, 45, 121, 125, 128, 246, 193, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 99, 35, 114, 51, 162, 86, 36, 33, 70, 65, 51, 69, 116, 52, 70, 70, 50, 83, 34, 54, 83, 49, 63, 67, 34, 50, 85, 109, 48, 245, 50, 98, 134, 49, 101, 1, 63, 95, 66, 101, 19, 51, 35, 55, 52, 113, 100, 82, 86, 18, 35, 114, 49, 20, 134, 67, 20, 115, 246, 36, 49, 68, 53, 19, 65, 33, 69, 52, 38, 65, 115, 19, 21, 53, 83, 68, 82, 50, 68, 113, 51, 67, 65, 47, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 211, 203]
What's the format of your template ?
Often template is a list of minutiae so it cannot be converted in image.
Take a look at https://en.wikipedia.org/wiki/Fingerprint

python - subtracting ranges from bigger ranges

The problem I have is that I basically would like to find if there are any free subnets between a BGP aggregate-address (ex: 10.76.32.0 255.255.240.0) and all the network commands on the same router (ex: 10.76.32.0 255.255.255.0, 10.76.33.0 255.255.255.0)
In the above example, 10.76.34.0 -> 10.76.47.255 would be free.
I'm thinking of tackling this problem by converting the IP addresses and subnet masks to binary and subtracting that way.
To keep it simple I will keep this example in decimal but doing this would leave me with the following problem: let's say I have a range from 1 to 250, I subtract from this a smaller range that goes from 20 to 23, I would like to end up with a range from 1 to 19 and 24 to 250.
Using the range command doesn't really give me the expected results and while I could possibly create a list with every item in the range and subtract another list with a sub-set of items, it seems to me that it might not be a good idea to have lists with possibly tens of thousands of elements.
Hunor
If you are trying to create a "range" with a gap in it, i.e., with 1-9 and 24-250, you could try to use filterfalse (or ifilterfalse if you are using Python 2.X) from the itertools module, which takes as its arguments a predicate and a sequence, and returns elements of the sequence where the predicate returns False. As an example, if you do:
from itertools import filterfalse
new_range = filterfalse(lambda x: 20 <= x <= 23, range(1,251))
new_range will be an iterable containing the numbers 1-19, and 24-250, which can be used similarly to range():
for i in new_range:
do_things()
The question has been asked long ago but I want to add numpy array answer.
import numpy as np
aa=np.arange(1,251)
bb=np.concatenate((np.array(aa[aa<20]),np.array(aa[aa>23])))
print(bb)
output
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173,
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238,
239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250])

Categories