DirectXTex Resize returns an empty ScratchImage - directx

I have this code
DirectX::ScratchImage DSResizedImage;
DirectX::Resize(*tex->DSImage.GetImage(0,0,0), tex->scaleX, tex->scaleY,
DirectX::TEX_FILTER_FLAGS::TEX_FILTER_DEFAULT, DSResizedImage);
DirectX::Image Resized1 = *DSResizedImage.GetImage(0, 0, 0);
which returns an empty Image.
However, it does work when using what should be the equivalent with OpenCV
std::string save = fullSavePath + tex->hash + ".PNG";
tex->tex2Other(fullSavePath + tex->hash + ".dds", "png");
cv::Mat cvIm = cv::imread(save, cv::IMREAD_UNCHANGED);
if (cvIm.empty())
{
printf("Tex not written!");
exit(1);
}
cvIm.copyTo(res(cv::Rect(tex->offsetX, tex->offsetY, tex->scaleX, tex->scaleY)));
The problem with using the OpenCV solution is that sometimes users will get this error when resizing (which I can't get to replicate on my computer in debug):
OpenCV(4.5.3) Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\matrix.cpp, line 811
For this specific image, it's dimensions (and scaleX/scaleY) are 2048x1024.
Any help would be very appreciated! :)

The DirectXTex function Resize was returning a failed HRESULT: E_NOINTERFACE. This happens when you use DirectXTex methods that use the Windows Imaging Component (WIC) without initializing COM on the calling thread.
Note that for Windows Runtime applications (such as UWP and/or C++/WinRT), COM is initialized as part of the Windows Runtime initialization.
This issue is documented on the DirectXTex wiki.

Related

Lua string::byte() returns nil in LuaJit 2.1.0-beta3

I didn't have this problem on LuaJit 2.1.0-beta2 but due to reasons had to update to beta3. Following code at some point generates an error on the line where "string.char(byte)" is called.
"bad argument #1 to ‘char’ (number expected, got nil)"
function convert_to_null_terminated_string(data, offset, len)
if data:len() < (offset + len - 1) then
print("Data len " .. data:len() .. " < (offset+len-1) " .. (offset + len - 1))
return nil
end
local output = {}
for i = 0, len - 1 do
local byte = data:byte(offset + i)
if byte == 0 then break end
table.insert(output, string.char(byte))
end
return table.concat(output, "")
Documentation about string:byte() doesn't mention it is possible that the function would return nil and I am sure I am within the boundaries of the string as the check is done in the function before calling string:byte().
It is worth mentioning that I compiled LuaJit with x64_GC flag to have a possibility to allocate more than 1-2GB of the memory in the Lua. All the code is running inside of the docker image based on Ubuntu 16 on Amazon Linux 2 host.
Does it looks like I am facing a bug in LuaJit?

iOS app crashing while scanning passport MRZ

The app is crashing while I am trying to scan the MRZ code on the passport. This is the log:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file ../matrix.cpp, line 517
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: ../matrix.cpp:517: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat
Here is the stack trace:
#12 0x0062662c in cv::Mat::operator()(cv::Rect_ const&) const
#13 0x00628c72 in ::-[PassportScanner realProcessImage:](cv::Mat &)
Contents of the above function are as follows:
Mat Mat::operator()( const Rect& roi ) const
{
return Mat(*this, roi);
}
-(void) realProcessImage:(cv::Mat &)cropped {
//Some other code
vector<cv::Rect> rotatedRegions = [self show:cropped gray:rotated2];
// Sort by y
std::sort(rotatedRegions.begin(), rotatedRegions.end(), sortByBoundingRectYPosition);
if (rotatedRegions.size() == 2) {
if(self.delegate)
[self.delegate setVisualIndicatorColor:[PassportScanner getColorForFraction:0.5]];
while (rotatedRegions[0].height + rotatedRegions[0].y >= rotated.rows) {
rotatedRegions[0].height--;
}
image1 = rotated(rotatedRegions[0]);
while (rotatedRegions[1].height + rotatedRegions[1].y >= rotated.rows) {
rotatedRegions[1].height--;
}
image2 = rotated(rotatedRegions[1]); //**——>Problem starts here**
//More code
}
I have looked at these links but none of them helped me solve the problem:
Error with OpenCV ROI
OpenCv assertion failed

OpenCV Error: Assertion failed (scn == 3 || scn == 4) when calling Core.inRange

I am having an assertion error when using the Core.inRange function, actually any Core. function. I have followed all solutions in the answers from similar questions. Other solutions have been to check the number of channels, check if the image is empty and verify installation. I am using Android Studio 2.2 on Mac. Phones tested were ZTE Speed KitKat and Moto g3 Marshmallow.
My goal is to get the red and blue from an image -> determine if a Red light is On or a Blue one is on. The code gets the image from a Vuforia Frame, converts it to a bitmap and then try to use OpenCV to manipulate the image. This was working on previous code before we had to implement Vuforia as part of the core.
This is the main section of the code, the Imgproc.cvtColor function works fine, its the very last one Core.inRange
Mat mat1 = new Mat(640,480, CvType.CV_8UC4);
Mat mat2 = new Mat(640,480, CvType.CV_8UC4);
Mat mat3 = new Mat(640,480, CvType.CV_8UC4);
.......
Log.d("OPENCV","Height " + rgb.getHeight() + " Width " + rgb.getWidth());
Bitmap bm = Bitmap.createBitmap(rgb.getWidth(), rgb.getHeight(), Bitmap.Config.RGB_565);
bm.copyPixelsFromBuffer(rgb.getPixels());
//Mat tmp = OCVUtils.bitmapToMat(bm, CvType.CV_8UC4);
Mat tmp = new Mat(rgb.getWidth(), rgb.getHeight(), CvType.CV_8UC4);
Utils.bitmapToMat(bm, tmp);
SaveImage(tmp, "-raw");
fileLogger.writeEvent("process()","Saved original file ");
Log.d("OPENCV","CV_8UC4 Height " + tmp.height() + " Width " + tmp.width());
Log.d("OPENCV","Channels " + tmp.channels());
tmp.convertTo(mat1, CvType.CV_8UC4);
Size size = new Size(640,480);//the dst image size,e.g.100x100
resize(mat1,mat1,size);//resize image
SaveImage(mat1, "-convertcv_8uc4");
Log.d("OPENCV","CV_8UC4 Height " + mat1.height() + " Width " + mat1.width());
fileLogger.writeEvent("process()","converted to cv_8uc4");
Log.d("OPENCV","Channels " + mat1.channels());
Imgproc.cvtColor(mat1, mat2, Imgproc.COLOR_RGB2HSV_FULL);
SaveImage(mat2, "-COLOR_RGB2HSV_FULL");
Log.d("OPENCV","COLOR_RGB2HSV Height " + mat2.height() + " Width " + mat2.width());
Log.d("OPENCV","Channels " + mat2.channels());
//Core.inRange(mat2, RED_LOWER_BOUNDS_HSV, RED_UPPER_BOUNDS_HSV, mat3);
Log.d("OPENCV","mat2 Channels " + mat2.channels() + " empty " + mat2.empty());
Log.d("OPENCV","mat3 Channels " + mat3.channels() + " empty " + mat3.empty());
Core.inRange(mat2, new Scalar(0,100,150), new Scalar(22,255,255), mat3);
fileLogger.writeEvent("process()","Set Red window Limits: ");
SaveImage(mat3, "-red limits");
These are the 2 errors I get when the command runs
E/cv::error(): OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/color.cpp, line 7349
E/org.opencv.imgproc: imgproc::cvtColor_10() caught cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/color.cpp:7349: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
3 images are saved in the pictures directory as expected.
My logging produces the following
D/OPENCV: mat2 Channels 3 empty false
D/OPENCV: mat3 Channels 4 empty false
I have tried two different phones, tried adjusting the resolution down. I have reinstalled the OpenCV module in case it was not installed correctly. I have made the images all 3 channels, all 4 channels.
So after a week of debugging it was the most stupidest of mistakes!
Within the SaveImage function
Imgproc.cvtColor(mat, mIntermediateMat, Imgproc.COLOR_RGBA2BGR, 3);
This was what was causing the issue.
After the Core.inRange was the SaveImage function. Core.inRange dropped the channels to 1 - the fileLogger did not flush the last log, If I had used Log instead I probably would have picked it quicker.
public void SaveImage (Mat mat, String info) {
Mat mIntermediateMat = new Mat();
Imgproc.cvtColor(mat, mIntermediateMat, Imgproc.COLOR_RGBA2BGR, 3); <--Here bad
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String filename = "ian" + info + ".png";
File file = new File(path, filename);
Boolean bool = null;
filename = file.toString();
bool = Imgcodecs.imwrite(filename, mIntermediateMat);
if (bool == true)
Log.d("filesave", "SUCCESS writing image to external storage");
else
Log.d("filesave", "Fail writing image to external storage");
}

Assertion failed when converting ROI to Mat

I am using OpenCV4Android version 2.4.11 I am detecting any rectangular shapes in frames retrieved by camera. for each detected rectangular shape I draw a rectangle arround it. Wha I am trying to do is, to convert the image inside
the rectangle into a Mat object then convert that Mat object int Bitmap and display that Bitmap object inside an ImageView as shown below in the code.
The problem is, when i run the below code sometimes the App crashes and displays the below posted logcat output. Actually i could not understand what does that logcat output mean but, i tryed to fix it by specifiying some conditions
in the if-statement shown in the code posted below but it did not solve the problem.
please let me know to solve this problem and why i am getting it?
code:
this.mBtnSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (bounding_rect != null && bounding_rect.width > 0 && bounding_rect.height > 0 && bounding_rect.width > bounding_rect.height && mMatInputFrame != null) {
Mat roi = new Mat(mMatInputFrame, bounding_rect); //<<<<---------- Line 160
Log.w(TAG, "bounding_rectw: " + bounding_rect.width + ", bounding_recth: " + bounding_rect.height);
Log.w(TAG, "ROIw: " + roi.width() + ", ROIh: " + roi.height());
final Bitmap bitmap = Bitmap.createBitmap(roi.width(), roi.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(roi, bitmap);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mIVCapture.setImageBitmap(bitmap);
}
});
}
}
});
logcat:
OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp, line 284
E/org.opencv.core.Mat: Mat::n_1Mat__JIIII() caught cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp:284: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.approxcontour_04, PID: 11436
CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp:284: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&)
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:676)
at com.example.approxcontour_04.FragOpenCVCam$2.onClick(FragOpenCVCam.java:150)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10930)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-25 10:51:21.271 11436-11672/com.example.approxcontour_04 D/CameraBridge: mStretch value: 0.0
I solved it by specifying the follwoing conditions in the if-statement
if (bounding_rect != null && mMatInputFrame.width() > bounding_rect.width
&& mMatInputFrame.height() > bounding_rect.height)

Error reading values from Mat

I am trying to implement HMM in opencv.
First i create arrays of double, and copy them to Mat variables,
Mat INIT = Mat(0,3,CV_64F,trans).clone();
Then i am trying to access the individual pixel/position values from the matrix as:
cout << INIT.at<double>(r,c) << " ";//Where r and c are row and column values.
I am getting error like:
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel
s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3
) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\opencv2.4.4\includ
e\opencv2\core\mat.hpp, line 537
I searched over the forums and couldnot find anything wrong with the code. Any ideas?
Thanks alot in advance.
Declare the Matrix INIT as :-
Mat INIT=Mat(1,3,CV_64FC1,trans).clone();
Now access the individual pixel/position values from the matrix as:
cout << INIT.at<double>(r,c) << " ";

Resources