Decrypting secure string with aes key - powershell
So I was given a key and a hash to decrypt with PowerShell.
key: (165, 49, 50, 151, 4, 58, 80, 217, 250, 19, 249, 150, 185, 102, 202, 113)
hash:
76492d1116743f0423413b16050a5345MgB8AEsAVQBkAGkAWQBQAGYAcgA2ADQASwBCAGQ
AcwBkAFEATQByAFQATABqAFEAPQA9AHwAOQBkAGQAYQA2AGQAYgA4ADQAZQBmAGUAMgBiADQ
ANgA1ADkANAA5AGYAMQBlADIAYgA5ADIAMQBmADUANgA0ADgANgBiADYANwBmAGMANgBjADAA
ZgA5AGUAZQAxAGQANgA3ADUANwA3ADUAZgA1AGYAOABlADcAOQA0AGMAOQA4ADMAZgBmADQ
AMQBkADcAYgAyADkAZQBhADEANwA2AGEAMQBhADMAZQBiADEAOQBkAGMANABiAGIANQBhAGM
AOQA0ADQAMwBhADAAZQAzAGQAZQAwADIANwA2ADUANwAzADcAMABhAGMAMwAxAGYAYwBjA
DYANAA5ADYAZgBlAGQANAA3ADIAMAA2AGUAOQA0ADEAMQA2ADEAYgA2ADMAYgAwADUANQA1A
GQAYQBiADQAOABkAGMANQAyADUAZABkADIANQA5AGQANQA3ADIANgA4ADUAYQA3ADcAMgAxAD
AAOQA2AGUAYQA4ADUAZQAyADgAZABhADMANABlADkANwA0ADIAZQBiADMAMwAzADMAMwAwA
DIAZAA4ADIAYgAwADkAZgBjADUAMgA4AGQANQBkADUAOQA0ADgAMwA4ADEAYgBmAGIAMwBjAGU
AYQAyADUAZgA3ADgAOQA0AGUAZABiAGMAYgBkADcANQAyADQAOQA0ADkANwAxADgAOQAxAGMA
ZAA5ADcAMQA2AGQAOABjAGQAOQAyAGIANQA0AGUAZgAzADcANwA0ADkAOQAxAGIANQA2AGUANw
AyADIAMAA5ADcAOQA3ADIANwAyADAAZgAyAGUAMgA2AGQAMwA1ADMAMAA1AGUAYwAxADcAYw
A2AGQAMwA2AGQAZQBjADgANwAyADYAYwBkADgANQA5AGYANwAwAGUAMQA3AGUANwBkADIAMQ
A4ADgANAA2ADIANAAwAGIANAA5ADgAMwBiADMAOAA1ADUAYgBiAGUAMABhAGIANgAxAGEAMwB
mADYAYQA5ADUANQBmAGUAZQAyADAAZQBhADAAZQAxADUAOAAxAGIAZQA1ADMAMAA5AGUAYQ
BiADkANwAwAGQAMQBmAGQAYgAxAGIANQA0ADAAYwAwADMAOQA5ADcANQA1ADcAOABjADUANg
AyADAAMAA2ADEAZAAyAGQAYgA0ADYAZAA2AGUAOABhADUANQA4ADgAYQA0AGMAZgAwADQANwB
lAGMAZABiAGQAYgBjADQANwA1AGQAYQA2ADQAYgA4ADcAMAAzADIAMgBhADIAZgA1ADAANgA4AGU
AYwBlADgAMgA4AGEAMwAzAGYAMAAzADkANQBlADkAMgBkADIAMgA1ADAAOAA4AGIAOQA3AGUAYg
AzADIAZQA3ADMAMQA4AA==
My code:
$Key = (165, 49, 50, 151, 4, 58, 80, 217, 250, 19, 249, 150, 185, 102, 202, 113)
ConvertFrom-SecureString (ConvertTo-SecureString
"abovehash" -AsPlainText -Force) -Key $Key
My theory is that it is already a secure string but to flip it to plaintext I would need to convertto and convertfrom to translate it. I used a website which decoded it perfectly
https://www.wietzebeukema.nl/powershell-securestring-decoder/#
I get the hash re-hashed again.
I know how to make my own secure key and decrypt it but how would you decrypt a hash if you were given the hash and the key.
I remember trying to do this 4 or 5 years ago to store random stuff in it. What I found out is for some reason the ConvertFrom-SecureString never worked. After some reasearch I found out converting to BTSR using the System.Runtime.InteropServices.Marshal SecureStringtoBTSR from a SecureString and then using the PtrToStringAuto will decrypt it properly. I will try to locate the reasoning and post it. But this should work and provide you the message you want:
$Key = (165, 49, 50, 151, 4, 58, 80, 217, 250, 19, 249, 150, 185, 102, 202, 113)
$hash = "76492d1116743f0423413b16050a5345MgB8AEsAVQBkAGkAWQBQAGYAcgA2ADQASwBCAGQAcwBkAFEATQByAFQATABqAFEAPQA9AHwAOQBkAGQAYQA2AGQAYgA4ADQAZQBmAGUAMgBiADQANgA1ADkANAA5AGYAMQBlADIAYgA5ADIAMQBmADUANgA0ADgANgBiADYANwBmAGMANgBjADAAZgA5AGUAZQAxAGQANgA3ADUANwA3ADUAZgA1AGYAOABlADcAOQA0AGMAOQA4ADMAZgBmADQAMQBkADcAYgAyADkAZQBhADEANwA2AGEAMQBhADMAZQBiADEAOQBkAGMANABiAGIANQBhAGMAOQA0ADQAMwBhADAAZQAzAGQAZQAwADIANwA2ADUANwAzADcAMABhAGMAMwAxAGYAYwBjADYANAA5ADYAZgBlAGQANAA3ADIAMAA2AGUAOQA0ADEAMQA2ADEAYgA2ADMAYgAwADUANQA1AGQAYQBiADQAOABkAGMANQAyADUAZABkADIANQA5AGQANQA3ADIANgA4ADUAYQA3ADcAMgAxADAAOQA2AGUAYQA4ADUAZQAyADgAZABhADMANABlADkANwA0ADIAZQBiADMAMwAzADMAMwAwADIAZAA4ADIAYgAwADkAZgBjADUAMgA4AGQANQBkADUAOQA0ADgAMwA4ADEAYgBmAGIAMwBjAGUAYQAyADUAZgA3ADgAOQA0AGUAZABiAGMAYgBkADcANQAyADQAOQA0ADkANwAxADgAOQAxAGMAZAA5ADcAMQA2AGQAOABjAGQAOQAyAGIANQA0AGUAZgAzADcANwA0ADkAOQAxAGIANQA2AGUANwAyADIAMAA5ADcAOQA3ADIANwAyADAAZgAyAGUAMgA2AGQAMwA1ADMAMAA1AGUAYwAxADcAYwA2AGQAMwA2AGQAZQBjADgANwAyADYAYwBkADgANQA5AGYANwAwAGUAMQA3AGUANwBkADIAMQA4ADgANAA2ADIANAAwAGIANAA5ADgAMwBiADMAOAA1ADUAYgBiAGUAMABhAGIANgAxAGEAMwBmADYAYQA5ADUANQBmAGUAZQAyADAAZQBhADAAZQAxADUAOAAxAGIAZQA1ADMAMAA5AGUAYQBiADkANwAwAGQAMQBmAGQAYgAxAGIANQA0ADAAYwAwADMAOQA5ADcANQA1ADcAOABjADUANgAyADAAMAA2ADEAZAAyAGQAYgA0ADYAZAA2AGUAOABhADUANQA4ADgAYQA0AGMAZgAwADQANwBlAGMAZABiAGQAYgBjADQANwA1AGQAYQA2ADQAYgA4ADcAMAAzADIAMgBhADIAZgA1ADAANgA4AGUAYwBlADgAMgA4AGEAMwAzAGYAMAAzADkANQBlADkAMgBkADIAMgA1ADAAOAA4AGIAOQA3AGUAYgAzADIAZQA3ADMAMQA4AA=="
$Secured = $hash | ConvertTo-SecureString -key $key
$BTSR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secured)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BTSR)
And of course in pure Microsoft fashion the explanation article is no longer where it used to be. Once I have the context reasoning I will update.
Update
Here is an SO Answer giving some context to it: Are you able to use PtrToStringAuto to decrypt a secure string in Powershell 7 on macOS?
Related
Convert from decimal to ASCII the data present in a column of a .csv file
i should convert the data of a column of a csv file in powershell This data is often in decimal and I would need to convert it to ASCII as in the example 80, 77, 79, 32, 83, 50, 52, 50, 45, 86, 70, 67 -> to Ascii > PMO S242-VFC the table is composed as follows Monitor Modello wkst1 80, 77, 79, 32, 83, 50, 52, 50, 45, 86, 70, 6 wkst2 V246HL wkst3 V256IL wkst4 65, 99, 101, 114, 32, 86, 50, 52, 54, 72, 76 this is the result Monitor Modello wkst1 PMO S242-VFC wkst2 V246HL wkst3 V256IL wkst4 Acer V246HL Thanks
You need to iterate your CSV file rows, and check each row, if the row is a Byte Data row, convert it using: [System.Text.Encoding]::ASCII.GetString So for example: foreach ($row in Import-Csv c:\filepath.csv) { if ($row.Modello -is [array] # just an example, needs more validation { [System.Text.Encoding]::ASCII.GetString([byte]$Row.Modello) # save it somewhere } } As you did not provided any code or showing your work, I'm just showing an example of what you can probably do,
Delphi 11: Constant object cannot be passed as var parameter
I am trying to transfer my Delphi 7 code to Delphi 11. The following code worked fine in Delphi 7, but in Delphi 11 I get a compile-time error [dcc32 Error] VELOS.PAS(44): E2197 Constant object cannot be passed as var parameter Is there a way to make Delphi 11 compile my code? I can make the initialized constants be global initialized var, but changing all similar code to comply with Delphi 11 is a lot of work because I use similar stuff a lot. My code: function Kt_f25(Mrd,Md,aro,dh:real; var k:real):real; const vmax=9; type Atype=array [1..vmax] of real; const arA:Atype=( 10, 15, 20, 25, 30, 40, 60, 100, 200); v0a08: Atype=( 235, 170, 135, 115, 100, 82, 63, 46, 33); v1a08: Atype=( 135, 102, 84, 74, 67, 57, 47, 37, 30); v0a09: Atype=( 166, 118, 95, 80, 70, 57, 44, 33, 23); v1a09: Atype=( 99, 76, 63, 56, 50, 43, 36, 29, 22); v0a10: Atype=( 120, 86, 70, 59, 51, 42, 32, 24, 17); v1a10: Atype=( 77, 59, 50, 44, 40, 34, 28, 22, 16); dhA:array [1..3] of real=(8,9,10); var v0,v1,v2,kt:real; res:array [1..3] of real; begin if Md>0 then k:=Mrd/Md else if Mrd=0 then k:=1 else k:=10; if k>1.3 then k:=1.3; if k<0 then k:=0; v0:=LinearApr(arA,v0a08,aro*1000,vmax); v1:=LinearApr(arA,v1a08,aro*1000,vmax); v2:=20; if k>=1 then res[1]:=v2+k*(v1-v2)/0.2 else res[1]:=v1+k*(v0-v1)/1.0; v0:=LinearApr(arA,v0a09,aro*1000,vmax); v1:=LinearApr(arA,v1a09,aro*1000,vmax); v2:=20; if k>=1 then res[2]:=v2+k*(v1-v2)/0.2 else res[2]:=v1+k*(v0-v1)/1.0; v0:=LinearApr(arA,v0a10,aro*1000,vmax); v1:=LinearApr(arA,v1a10,aro*1000,vmax); v2:=20; if k>=1 then res[3]:=v2+k*(v1-v2)/0.2 else res[3]:=v1+k*(v0-v1)/1.0; kt:=LinearApr(dhA,res,dh*10,3); Result:=kt/10; end; function LinearApr(var ap1,AV1; r:real; Vmax:integer):real; const gmax=100; type Atype=array [1..gmax] of real; var i,j:integer; ap2:Atype absolute ap1; AV2:Atype absolute AV1; ap,AV:Atype; begin ... end;
The error is self-explanatory. You are passing typed constants where variable references are expected. You are encountering the following change in behavior between Delphi 7 and 11: Writeable typed constants (Delphi) The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {$J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error. ... In early versions of Delphi and Object Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state. The default state is {$J-} in modern Delphi versions. So, simply add an explicit {$J+} or {$WRITEABLECONST ON} directive to your existing code to get the old behavior.
Extracting Temperatures from .ravi file in Matlab
My Problem Much like the post here: How can I get data from 'ravi' file?, I have a .ravi file (a radiometric video file, which is rather similar to an .avi) and I am trying to extract the Temperatures in it, to use them together with additional sensor data. A Sample File can be found in the documentation (http://infrarougekelvin.com/en/optris-logiciel-eng/) when you download the "PIX Connect Software". Unfortunately, according to the documentation, the temperature information is stored in a 16 Bit Format, that Matlab seems to be rather unhappy with. How I tried to solve my problem I tried to follow the instructions from the before mentioned post, but I somehow struggle to reach results, which are even close to the correct temperatures.Original Picture with temperatures in the Optris Software I tried to read the video with different methods: At first I hoped to use the videorecorder Feature in Matlab: video = VideoReader(videoPath); frame1 = video.read(1); imagesc(frame1) But it only resulted in this poor picture, which is exactly what I can see, when I try to play the .ravi file in a media player like vlc. First try with videorecorder function Then I tried to look at the binary representation of my file and noticed, that I could separate the frames at a certain marker Beginning of a new frame in binary representation So I tried to read the file with the matlab fread function: fileID = fopen(videoPath); [headerInfo,~] = fread(fileID,[1,123392],'uint8'); [imageMatrix,count] = fread(fileID,[video.width, video.height],'uint16', 'b'); imagesc(imageMatrix') Now the image looks better, and you can at least see the brake disc, but it seems, as if the higher temperatures have some kind of offset, that is stil missing, for the picture to be right. Also, the values that I read from the file are nowhere near actual temperatures, as the other post and the documentation suggests. Getting somewhere! My Question Am I somehow missing something important? Could someone point me in the right direction, where to look or how to get the actual temperatures from my video? As it worked with the cpp code in the other post, I am guessing this might be a matlab problem.
A relatively simple solution for getting the raw frame data is converting the RAVI video file to raw video file format. You can use FFmpeg (command line tool) for converting the RAVI to RAW format. Example: ffmpeg -y -f avi -i "Sequence_LED_Holder.ravi" -vcodec rawvideo "Sequence_LED_Holder.yuv" The YUV (raw binary data) file, can be simply read by MATLAB using fread function. Note: the .yuv is just a convention (used by FFmpeg) for raw video files - the actual pixel format is not YUV, but int16 format. You can try parsing the RAVI file manually, but using FFmpeg is much simpler. The raw file format is composed of raw video frames one after the other with no headers. I our case, each frame is width*height*2 bytes. The pixel type is int16 (may include negative values). The IR video frames has no color information. The colors are just "false colors" created using palette and used for visualization. The code sample uses a palette from different IR camera manufacture. Getting the temperature: I could not find the way to convert the pixel value to the equivalent temperature. I didn't read the documentation - there is a chance that the conversion is documented somewhere. The MATLAB code sample applies the following stages: Convert RAVI file format to RAW video file format using FFmpeg. Read video frames as [cols, rows] size int16 matrix. Remove the first line that probably contains data (not pixels). Use linear contrast stretch - for visualization. Apply false colors - for visualization. Display the processed video frame. Here is the code sample: %ravi_file_name = 'Brake disc.ravi'; %ravi_file_name = 'Combustion process.ravi'; %ravi_file_name = 'Electronic board.ravi'; %ravi_file_name = 'Sequence_carwheels.ravi'; %ravi_file_name = 'Sequence_drop.ravi'; ravi_file_name = 'Sequence_LED_Holder.ravi'; %ravi_file_name = 'Steel workpiece with hole.ravi'; yuv_file_name = strrep(ravi_file_name, '.ravi', '.yuv'); % Same file name with .yuv extension. % Get video resolution. vidinfo = mmfileinfo(ravi_file_name); cols = vidinfo.Video.Width; rows = vidinfo.Video.Height; % Execute ffmpeg (in the system shell) for converting RAVI to raw data file. % Remark: download FFmpeg if needed, and make sure ffmpeg executable is in the execution path. if ~exist(yuv_file_name, 'file') % Remark: For some of the video files, cmdout returns a string with lots of meta-data [status, cmdout] = system(sprintf('ffmpeg -y -f avi -i "%s" -vcodec rawvideo "%s"', ravi_file_name, yuv_file_name)); if (status ~= 0) fprintf(cmdout); error(['Error: ffmpeg status = ', num2str(status)]); end end % Get the number of frames according to file size. filesize = getfield(dir(yuv_file_name), 'bytes'); n_frames = filesize / (cols*rows*2); f = fopen(yuv_file_name, 'r'); % Iterate the frames (skip the last frame). for i = 1:n_frames-1 % Read frame as cols x rows and int16 type. % The data is signed (int16) and not uint16. I = fread(f, [cols, rows], '*int16')'; % It looks like the first line contains some data (not pixels). data_line = I(1, :); I = I(2:end, :); % Apply linear stretch - in order to "see something"... J = imadjust(I, stretchlim(I, [0.02, 0.98])); % Apply false colors - just for visualization. K = ColorizeIr(J); if (i == 1) figure; h = imshow(K, []); %h = imshow(J, []); impixelinfo else if ~isvalid(h) break; end h.CData = K; %h.CData = J; end pause(0.05); end fclose(f); imwrite(uint16(J+2^15), 'J.tif'); % Write J as uint16 image. imwrite(K, 'K.png'); % Write K image (last frame). % Colorize the IR video frame with "false colors". function J = ColorizeIr(I) % The palette apply different IR manufacture - don't expect the result to resemble OPTRIS output. % https://groups.google.com/g/flir-lepton/c/Cm8lGQyspmk colormapIronBlack = uint8([... 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]); lutR = colormapIronBlack(1:3:end); lutG = colormapIronBlack(2:3:end); lutB = colormapIronBlack(3:3:end); % Convert I to uint8 I = im2uint8(I); R = lutR(I+1); G = lutG(I+1); B = lutB(I+1); J = cat(3, R, G, B); end Sample output: Update: Python code sample using OpenCV (without colorizing): Using Python and OpenCV, we may skip the FFmpeg conversion part. Instead of converting the RAVI file to YUV file, we may fetch undecoded RAW video from the RAVI file. Open a video file and set CAP_PROP_FORMAT property for fetch undecoded RAW video: cap = cv2.VideoCapture(ravi_file_name) cap.set(cv2.CAP_PROP_FORMAT, -1) # Format of the Mat objects. Set value -1 to fetch undecoded RAW video streams (as Mat 8UC1). When reading a video frame (using ret, frame = cap.read()), the undecoded frame is read as a "long" row vector of uint8 elements. Converting the frame to int16 type, and reshaping to cols x rows: First, we have to "view" the vector elements as int16 elements (opposed to uint8 elements): frame.view(np.int16) Second, we have to reshape the vector into a matrix. Conversion and reshaping code: frame = frame.view(np.int16).reshape(rows, cols) Complete Python code sample: import numpy as np import cv2 ravi_file_name = 'Sequence_LED_Holder.ravi' cap = cv2.VideoCapture(ravi_file_name) # Opens a video file for capturing # Fetch undecoded RAW video streams cap.set(cv2.CAP_PROP_FORMAT, -1) # Format of the Mat objects. Set value -1 to fetch undecoded RAW video streams (as Mat 8UC1). [Using cap.set(cv2.CAP_PROP_CONVERT_RGB, 0) is not working] cols = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Get video frames width rows = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Get video frames height while True: ret, frame = cap.read() # Read next video frame (undecoded frame is read as long row vector). if not ret: break # Stop reading frames when ret = False (after the last frame is read). # View frame as int16 elements, and reshape to cols x rows (each pixel is signed 16 bits) frame = frame.view(np.int16).reshape(rows, cols) # It looks like the first line contains some data (not pixels). # data_line = frame[0, :] frame_roi = frame[1:, :] # Ignore the first row. # Normalizing frame to range [0, 255], and get the result as type uint8 (this part is used just for making the data visible). normed = cv2.normalize(frame_roi, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) cv2.imshow('normed', normed) # Show the normalized video frame cv2.waitKey(10) cap.release() cv2.destroyAllWindows() Sample output: Note: In case colorization is required, you may use the following example: Thermal Image Processing
In most of ravi files processed with Ffmpeg, there are non-pixel values on the first line of the raw image. This first line stores some redondant information such as image width and height. We have to skip this line which corresponds to the image width. Since data values are 16-bit, we must multiply by 2, to get the exact offset of the binary data. We have also to calculate the exact size of the image: imageLength = Frame size - (image width * 2). In the other case, data are from the start of the file and we can use the frame size (w * h * 2) to copy binary data and update the offset. To know if it's necessary to calculate the data offset, we just look at image height. If this value is odd, that means there is a supplementary first line and thus we apply the correction. If the value is even, no correction for the data offset. This is the same story when parsing the original ravi files. First we have to find the offset of the movi tag in the file. If the movi tag if followed by the ix00 tag, that means we have just after a series of values that give the offset and the size of each frames from the offset of the movi tag. Real data are elsewere in the file. If ix00 tag is not present, that means that data are just inside the movi chunck, after the 00db flag, and frame by frame. In this last case, we can also look for the idx1 tag (at the end of the file) which gives access to the exact offset and size of each frame. Both approaches allow a rather correct image representation in grayscale or in pseudo-color, but the temperature formula provided by the libirimager tool-kit (float t = (float)data[i] / 10.f - 100.f) is incorrect and I do not undestand why, since the formula was correct when I was using raw data produced by the PI-160 camera. Fmmpeg test I found an alternative way. In recent ravi Optris file we can get the temperature range in the INFO chunk. Then, it's easy to find the minimal and maximal values in raw data and to interpolate in reference to the temperature scale. with correct temperatures
Each frame holds 16-bit values by pixel with low byte first and high byte after. To find the temperature you have to apply this formula: temp = (hi * 256.0 + lo) / 10.0 - 100.0. With low value, you can create a grayscale image. I used this approach with old Pi-160 Optris camera with success. However with new PI-450, it's more difficult since PI Connect does not support binary export now. I tested the solution with Ffmpeg without success. You get a 16-bit data file, but the offset of real data is incorrect, and thus the temperature is aberrant. Did you succeed ? Sample of binary reading:
How to encode utf8 code units to String and read back Flutter
My user input is a string, which also has emojis, so I have converted it to a list of utf8 code units. When I send this response to server, it stores it as a String "[60, 115, 116, 114, 111, 110, 103, 62, 114, 105, 99, 104, 32, 116, 101, 120, 116, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152, 137, 60, 47, 115, 116, 114, 111, 110, 103, 62, 60, 98, 114, 62, 60, 98, 114, 62]" but when I try to decode this, I cannot as utf8.decode(CodeUnits) require a List input but my input is now a String. I am encoding this String to utf8 using: utf8.encode("My String here😀"); Please let me know if there is some better approach to storing and displaying emojis. We are using Django Rest Framework in our API.
Calling your string temp, you can convert temp to a List by: List<int> intList = List<int>.from(json.decode(temp)); Then just convert it to UTF8 using: String result = new Utf8Decoder().convert(intList); print(result); Result: <strong>rich text😉😉😉😉😉</strong><br><br> A more dense answer would be: String result = new Utf8Decoder().convert(List<int>.from(json.decode(temp)))
How can I convert a compressed .gz file that I'm getting from a bluetooth Low-energy device to an actual decompressed file in Swift 4.2, using gzip?
I am very new to Xcode and iOS, I have a device, let's call it Brains, that I'm connecting to via Bluetooth LE using an app I built with Swift 4 and Xcode 10 on my iPhone 5, call it Body. Brains is similar to an arduino board, but not exactly. I can connect and get all the data with BLE with no problems, until I tried to get a compressed file filled with json strings. I am receiving the compressed bytes but I can't seem to know what to do next. How can I get the compressed file, decompress it and read the data inside? I have tried many things from using the Modules: GzipSwift, DataCompression and SSZipArchive I have used gunzipped(), gunzip() and decompress() but none of them seem to work. I have read this thread: iOS :: How to decompress .gz file using GZIP Utility? and it say that I have to get all the compressed bytes stream and convert that to NSData and then decompress it, trouble is he's Using objective-c and I cant seem to translate into swift 4. I'm getting the bytes from the Bluetooth LE characteristic in a [UInt8] array, in this function: func received_logs(data: [UInt8]) { let data_array_example = [31, 139, 8, 8, 16, 225, 156, 92, 2, 255, 68, 97, 116, 97, 0, 181, 157, 107, 110, 220, 56, 16, 6, 175, 226, 3, 248, 71, 63, 73, 234, 44, 193, 222, 255, 26, 171, 30, 35, 192, 90, 20, 18, 121, 182, 11, 112, 16, 35, 48, 10, 31, 154, 197, 22, 135, 34, 227, 95, 191, 76, 244, 16, 183, 248, 252, 48, 137, 229, 38, 242, 249, 161, 231, 87, 156, 127, 207, 113, 126, 227, 159, 31, 231, 183, 110, 223, 255, 200, 239, 47, 203, 252, 253, 173, 255, 231, 159, 235, 235, 108, 105, 110, 101, 48, 47, 50, 48] for data_byte in stride(from: 0, to: data_array_example.count, by: 1) { let byte = String(data_array_example[data_byte]) sourceString = sourceString + byte //getting all the bytes and converting to string to store in a variable } /******************************************************************/ let text = sourceBuffer do { try text.write(to: path!, atomically: false, encoding: String.Encoding.utf8) } catch { print("Failed writing") } //dump the var into a txt file /**********UPDATED**********/ var file_array : [UInt8] = [] let byte2 = NSData(data: data_array_example.data) let asc_array = Data(bytes: byte2.data) let decompressedData: Data do { try decompressedData = asc.gunzipped() print("Decom: ", String(data: decompressedData, encoding: .utf8)) } catch { print(error) //Gives me the "unknown compression method error" } } I expect to see the Uncompressed file's contents but I only get: GzipError(kind: Gzip.GzipError.Kind.data, message: "incorrect header check") Maybe I'm just making it more complicated than It needs to be. Any help would be greatly appreciated! Thank you very much :) UPDATE: I created a .gz file and used the both the gunzipped() and gunzip() functions and both of them worked. UPDATE: Tried to directly convert the data to NSData and then gunzip() but now getting the error: GzipError(kind: Gzip.GzipError.Kind.data, message: "unknown compression method")
The updated example data has a correct gzip header, and so would not be giving you an incorrect header check if you are feeding the data correctly to the gunzipper.
I solve my issue. Turns out I was miscounting the bytes and some of them were in the wrong order. Thank you guys for your help!