I need to use open cv functions: cv2.imencode,cv2.imdecode to compress (jpeg) and decompress (jpeg) for different QF values.
The picture is 'bridge.ppm' from https://imagecompression.info/test_images/
I've tried:
bridge = cv2.imread('./bridge.ppm')
bridge_en = cv2.imencode('.jpeg', bridge)
bridge_de = cv2.imdecode('.jpeg', bridge_en)
cv2.imshow('image',bridge_de)
but I'm getting an error in the 2nd line saying: "Expected Ptr<cv::UMat> for argument 'buf'".
Also, how can I change and test different QF values?
Please take a look to the documentation for imencode and imdecode
imencode returns two values, the encoded buffer is the second one. And imdecode accepts the encoded buffer and a flag. So:
bridge = cv2.imread('./bridge.ppm')
bridge_en = cv2.imencode('.jpeg', bridge)[1] # you need the second value
bridge_de = cv2.imdecode(bridge_en, cv2.IMREAD_UNCHANGED) # or any other flag, same as 'imread'
cv2.imshow('image',bridge_de)
Related
I'm using opencv-python in VSCODE, I create 2 2-dimensions arrays of the same type and same size. but I got this error when it executes the method cv2.addWeighted, I see other people are asking the same question, but their problem is the different data type. However, I have double checked the data type in my code, they are the same (np.uint8).
img01 = np.ones((200, 200), dtype=np.uint8) * 1000
img02 = np.ones((200, 200), dtype=np.uint8) * 100
weightedImg = cv2.addWeighted(img01,0.6,img02,0.4,3)
error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'cv::arithm_op'
I would like to know what's wrong in my code. thanks.
I have a slice that I have guaranteed (in runtime) that its length is at least 8. I want to convert that slice to an array because I need to use std.mem.bytesAsValue() in order to create an f64 from raw bytes (for context, I'm implementing a binary serialization format).
I solved it like this, but I'd like to know if there is a better syntax for achieving the same goal:
var array: [8]u8 = undefined;
array[0] = slice[0];
array[1] = slice[1];
array[2] = slice[2];
array[3] = slice[3];
array[4] = slice[4];
array[5] = slice[5];
array[6] = slice[6];
array[7] = slice[7];
Given your goal, if alignment works out, you could just cast the slice pointer to a pointer to a f64: #ptrCast(*f64, slice.ptr). This saves you from even having to copy anything, in case you don't need to do that at all.
Note that bytesAsValues is a stdlib function that uses compiler builtins, if the signature doesn't match your needs, you can just skip it and use directly the builtins, like I mentioned above. Don't be afraid to read the stdlib source code.
Here's a godbolt link with some examples on how to do the conversion in-place (by casting the pointer) and by copying everything immediately as a float: https://zig.godbolt.org/z/3Y9o5zvfe
you can do this by std.mem.copy
var array = slice[0..8].*;
or using std.mem.copy
std.mem.copy(u8, &array, slice[0..2]);
while i think you can just need to put slice[0..8] as argument instead creating a variable
I want to apply a log function to images. But it fails showing this error: function is not defined on this type of argument.
uk=imread('image.jpg');
result=log(uk(:,:,1));
I think your problem is that imread returns a matrix of uint8 type. To apply log, you should convert it to double. There are at least 2 ways to do this, one built in, and one from SIVP:
clc;
clear;
im = imread("d:\Attila\PROJECTS\Scilab\Stackoverflow\mixer_crop.jpg");
//imshow(im);
disp(typeof(im(:,:,1)),"Original type:");
//use double
M = double(im(:,:,1));
disp(typeof(M),"Modified type:");
result=log(M);
//imshow(uint8(M));
//use im2double
M2 = im2double(im);
disp(typeof(M2(:,:,1)),"Modified type 2:");
result=log(M2(:,:,1));
//imshow(im2uint8(M2));
I am trying to save some tables of strings to files in Torch. I have tried using this Torch extension by Deepmind: hdf5.
require 'hdf5'
label = {'a', 'b','c','d'}
local myFile = hdf5.open(features_repo .. 't.h5', 'w')
myFile:write('label', label)
myFile:close()
I am getting the error:
/home/user/torch/install/bin/luajit: ...e/user/torch/install/share/lua/5.1/hdf5/group.lua:222: torch-hdf5: writing data of type string is not supported
Torch Tensors are written to file as intended.
I have also tried using matio to write to mat files (for MatLab). I am getting this error:
bad argument #1 to 'varCreate' (cannot convert 'number' to 'const char *')
The error is because "label" is a table of strings, but the function HDF5Group:_writeData is expecting a form of "tensor".
Looking at ffi.lua, it seems that the "tensor" is a typedef for "integer", so maybe replace:
label = {'a', 'b','c','d'}
with
label = {1,2,3,4}
You can use the function t2s from the module (https://github.com/aryajur/tableUtils.git) to generate a string you can save to a file. To convert back just use function s2t.
I am adapting an old code which uses cvMat. I use the constructor from cvMat :
Mat A(B); // B is a cvMat
When I write A[i][j], I get the error no operator [] match these operands.
Why? For information: B is a single channel float matrix (from a MLData object read from a csv file).
The documentation lists the at operator as being used to access a member.
A.at<int>(i,j); //Or whatever type you are storing.
first, you should have a look at the most basic opencv tutorials
so, if you have a 3channel, bgr image (the most common case), you will have to access it like:
Vec3b & pixel = A.at<Vec3b>(y,x); // we're in row,col world, here !
pixel = Vec3b(17,18,19); // at() returns a reference, so you can *set* that, too.
the 1channel (grayscale) version would look like this:
uchar & pixel = A.at<uchar>(y,x);
since you mention float images:
float & pixel = A.at<float>(y,x);
you can't choose the type at will, you have to use, what's inside the Mat, so try to query A.type() before.