英文:
Why does cv2.threshold sometimes reduce the dimension of a numpy array?
问题
Originally coming from a brain segmentation task i have a numpy array, which is getting thresholded via cv2.threshold. However, the thresholding reduces the dimension of the array which seems to contradict the documentation of cv2.threshold. For many other arrays coming from the same brain segmentation tool the dimension does not get reduced. Also after sorting the image linewise (mainly to enhance anonymity) the dimension gets reduced.
cv2.version returns 4.1.2
Does anyone have an idea how this might come, should i open a bug report?
If necessary i could also share the sorted_strange_array.npy
Input:
import numpy as np
import cv2
strange_array = np.load("PATH/sorted_strange_array.npy")
print(strange_array.shape)
retVal, thresholded_array = cv2.threshold(strange_array, thresh=0.5, maxval=1, type=cv2.THRESH_BINARY)
print(thresholded_array.shape)
Output:
(163, 1024, 1024)
(163, 1024)
4.1.2
I was expecting the output to be
(163, 1024, 1024)
(163, 1024, 1024)
4.1.2
英文:
Originally coming from a brain segmentation task i have a numpy array, which is getting thresholded via cv2.threshold. However, the thresholding reduces the dimension of the array which seems to contradict the documentation of cv2.threshold. For many other arrays coming from the same brain segmentation tool the dimension does not get reduced. Also after sorting the image linewise (mainly to enhance anonymity) the dimension gets reduced.
cv2.version returns 4.1.2
Does anyone have an idea how this might come, should i open a bug report?
If necessary i could also share the sorted_strange_array.npy
Input:
import numpy as np
import cv2
strange_array = np.load("PATH/sorted_strange_array.npy")
print(strange_array.shape)
retVal, thresholded_array = cv2.threshold(strange_array, thresh=0.5, maxval=1, type=cv2.THRESH_BINARY)
print(thresholded_array.shape)
Output:
(163, 1024, 1024)
(163, 1024)
4.1.2
I was expecting the output to be
(163, 1024, 1024)
(163, 1024, 1024)
4.1.2
答案1
得分: 3
OpenCV支持的通道数限制为512通道。
该限制在OpenCV源文件interface.h中定义:
#define CV_CN_MAX 512
发布的矩阵的形状是(163, 1024, 1024)
,因此具有1024
个通道,超过了512
的限制。
深入研究OpenCV的Python绑定,并找到减少通道数的确切位置是一项具有挑战性的任务。
也许Python OpenCV绑定有一些保护措施,也许这是一个bug...
我尝试在C++中重现这个问题:
#include <opencv2/opencv.hpp>
int main()
{
//https://stackoverflow.com/a/11034222/4926757
cv::Mat strange_array = cv::Mat(163, 1024, CV_8UC(1024));
strange_array.setTo(100);
cv::Mat threshold;
cv::threshold(strange_array, threshold, 100, 255, cv::THRESH_BINARY);
return 0;
}
上述代码中threshold
的大小为1024x163,具有512个通道。strange_array
的大小也为1024x163,具有512个通道。
当使用cv::Mat(163, 1024, CV_8UC(1023))
时,通道数为511
(没有保护时会发生溢出)。
总的来说,OpenCV支持最多512个通道的矩阵,因此我们不能说这个问题是一个bug。
英文:
It looks like OpenCV supported number of channels is limited to 512 channels.
The limitation is defined in the OpenCV source file interface.h
<!-- language: lang-c++ -->
#define CV_CN_MAX 512
The shape of the posted matrix is (163, 1024, 1024)
, so it has 1024
channels, that exceeds the 512
limitation.
Digging into the Python binding of OpenCV, and finding the exact place where the number of channels is reduced is a challenging task.
Maybe the Python OpenCV binding has some protection, and maybe it's a bug...
I tried to reproduce the issue in C++:
<!-- language: lang-c++ -->
#include <opencv2/opencv.hpp>
int main()
{
//https://stackoverflow.com/a/11034222/4926757
cv::Mat strange_array = cv::Mat(163, 1024, CV_8UC(1024));
strange_array.setTo(100);
cv::Mat threshold;
cv::threshold(strange_array, threshold, 100, 255, cv::THRESH_BINARY);
return 0;
}
The size of threshold
in the above code is 1024x163 with 512 channels.
The size of strange_array
is also 1024x163 with 512 channels.
When using cv::Mat(163, 1024, CV_8UC(1023))
there are 511
channels (there is an overflow without protection).
In general OpenCV supports matrices with up to 512 channels, so we can't say that the issue is a bug.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论