- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我正在浏览 Robert Laganiere 的“OpenCV 2 计算机视觉应用程序编程指南”。在第 42 页左右,它正在谈论一种图像缩小算法。我理解算法(我认为)但我不明白为什么要放入一个部分。我想我知道为什么但如果我错了我想纠正。我将在此处复制并粘贴其中的一些内容:
"Color images are composed of 3-channel pixels. Each of these channels corresponds to the intensity value of one of the three primary colors (red, green, blue). Since each of these values is an 8-bit unsigned char, the total number of colors is 256x256x256, which is more than 16 million colors. Consequently, to reduce the complexity of an analysis, it is sometimes useful to reduce the number of colors in an image. One simple way to achieve this goal is to simply subdivide the RGB space into cubes of equal sizes. For example, if you reduce the number of colors in each dimension by 8, then you would obtain a total of 32x32x32 colors. Each color in the original image is then assigned a new color value in the color-reduced image that corresponds to the value in the center of the cube to which it belongs. Therefore, the basic color reduction algorithm is simple. If N is the reduction factor, then for each pixel in the image and for each channel of this pixel, divide the value by N (integer division, therefore the reminder is lost). Then multiply the result by N, this will give you the multiple of N just below the input pixel value. Just add N/2 and you obtain the central position of the interval between two adjacent multiples of N. if you repeat this process for each 8-bit channel value, then you will obtain a total of 256/N x 256/N x 256/N possible color values. How to do it... The signature of our color reduction function will be as follows: void colorReduce(cv::Mat &image, int div=64); The user provides an image and the per-channel reduction factor. Here, the processing is done in-place, that is the pixel values of the input image are modified by the function. See the There's more... section of this recipe for a more general function signature with input and output arguments. The processing is simply done by creating a double loop that goes over all pixel values: "
void colorReduce(cv::Mat &image, int div=64) {
int nl= image.rows; // number of lines
// total number of elements per line
int nc= image.cols * image.channels();
for (int j=0; j<nl; j++) {
// get the address of row j
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
// process each pixel ---------------------
data[i]=
data[i]/div*div + div/2;// <-HERE IS WHERE I NEED UNDERSTANDING!!!
// end of pixel processing ---------------
}}}
所以我明白了如何将 0:255 像素值减少 div 数量。然后我失去了剩下的任何剩余部分。然后再次将它乘以 div 数量,我们将它按比例放大以保持在 0:255 的范围内。 为什么我们然后将 (div/2) 添加回答案? 我能想到的唯一原因是这会导致某些值向下舍入,而某些值向上舍入。如果您不使用它,那么您的所有值都会向下舍入。所以在某种程度上它给出了一个“更好”的平均值?
不知道,你们觉得怎么样?
最佳答案
说明这一点的最简单方法是使用示例。
为简单起见,假设我们正在处理图像的单个 channel 。有 256 种不同的颜色,范围从 0 到 255。我们还将在示例中使用 N=64。
使用这些数字,我们会将颜色的数量从 256 减少到 256/64 = 4。让我们画一个颜色空间图:
|......|......|......|......|
0 63 127 191 255
虚线表示我们的色彩空间,从 0 到 255。我们将这个区间分成 4 个部分,分割由垂直线表示。
为了将所有 256 种颜色减少为 4 种颜色,我们将每种颜色除以 64(失去余数),然后再乘以 64。让我们看看这是怎么回事:
[0 , 63 ] / 64 * 64 = 0
[64 , 127] / 64 * 64 = 64
[128, 191] / 64 * 64 = 128
[192, 255] / 64 * 64 = 192
如您所见,第一部分的所有颜色变为 0,第二部分的所有颜色变为 64,第三部分 128,第四部分 192。所以我们的颜色空间如下所示:
|......|......|......|......|
0 63 127 191 255
|______/|_____/|_____/|_____/
| | | |
0 64 128 192
但这不是很有用。你可以看到我们所有的颜色都向间隔的左侧倾斜。如果他们在间隔的中间会更有帮助。这就是我们将 64/2 = 32
添加到值的原因。添加一半的间隔长度会将颜色移动到间隔的中心。书上也是这么说的:“只要加上 N/2,你就得到了 N 的两个相邻倍数之间的间隔的中心位置。”
所以让我们将 32 添加到我们的值中,看看一切看起来如何:
[0 , 63 ] / 64 * 64 + 32 = 32
[64 , 127] / 64 * 64 + 32 = 96
[128, 191] / 64 * 64 + 32 = 160
[192, 255] / 64 * 64 + 32 = 224
间隔看起来像这样:
|......|......|......|......|
0 63 127 191 255
\______/\_____/\_____/\_____/
| | | |
32 96 160 224
这是一个更好的颜色还原。该算法将我们的色彩空间从 256 种颜色减少到 4 种颜色,并且这些颜色位于它们减少的间隔的中间。
关于c++ - 为什么这个 "reduction factor"算法在做 "+ div/2",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23919916/
我对流动的两种情况下变量 acc 的数据共享范围感到困惑。在情况 1 中,我收到以下编译错误:error: reduction variable ‘acc’ is private in outer c
我正在研究这个 Verilog 文件: `default_nettype none module stroboscope(i_clk, o_led); input wire i_clk
我正在为即将到来的 Haskell 考试复习,但我不明白过去论文中的一个问题。谷歌出现 nothing useful fst(x, y) = x square i = i * i i) Source
LEt x_t = F(x_{t-1}) 是 chaotic regime. 中的一个时间离散动力系统 从初始条件x_0开始,我们可以生成一个时间序列=x_t,其中t =1,2,...,T 表示时间索
我正在尝试使用 OpenMP 并行化 vector 点积程序。下面的代码显示了我所做的。 #define N 1000000 float dotProduct = 0; float vector1Ho
我有一个需要以下内容的项目。 代码中将声明四个数组,如下所示: var ROW1 = ['module1']; var ROW2 = ['module2', 'module3']; var ROW3
我是 opencl 的新手。我试过“获取数组中每个元素的所有立方体的总和”。这是我的内核代码: kernel void cubeSum(global float *input,
在 C99 规范中它说 remquo: The remquo functions are intended for implementing argument reductions which can
我正在关注'Learn Haskell Fast and Hard'我能够理解其中的大部分内容,但我对以下代码示例有两个问题。 在第一个函数中,为什么我不需要 l 但在第二个版本中我确实需要 l? 在
我需要更新数据框中的一些数据,就像 SQL 中的更新查询一样。我当前的代码如下: import pandas df = pandas.read_csv('filee.csv') # load trad
我有函数的当前版本: void* function(const Input_st *Data, Output_st *Image) { int i,j,r,Of
目前正在尝试使用 CUDA pdf 中的 Reduction #3 outline here . 这是我的 Reduction 函数的样子 template __device__ void offs
我正在尝试使用官方 CUDA 缩减 PDF 中讨论的缩减内核之一 here .但是,我不明白它是如何工作的,除非我遗漏了一些似乎没有多大意义的东西。 这是我的内核: __global__ void e
Please click this to see my problem 嗨。 关于这个问题,我只是看不懂它提供的解决方案。 我们知道 Atm 的补码 = { : M是TM,M不接受W}和照片中描述的
我已经看到各种讨论和代码尝试来解决 "String reduction"来自 interviewstreet.com 的问题,但没有一个是通过动态规划来解决的。 在 Dynamic Programmi
我正在尝试对 zip 迭代器进行最小缩减,但使用自定义运算符仅考虑元组中的第二个字段(第一个字段是键,而第二个字段是值)实际上与减少有关) 但是,我无法让它工作,目前正在计算 vector 中存在的结
这个问题在这里已经有了答案: OpenMP in C array reduction / parallelize the code (1 个回答) 关闭去年。 我正在尝试使用 #pragma omp
我有一种用 PLT-Redex 定义的语言,它具有(动态)mixin 类型。表达式如下所示: ; terms / expressions (e ::= x (lkp e f) (c
我正在研究代码 war 中的方向减少问题,但我无法弄清楚它给我带来的错误。我知道也有类似的情况,但是当我在 Visual Studio Code 上测试我的代码时,它工作得完美无缺,所以我不确定为什么
我用 C++ 和 CUDA 编写的 TensorFlow r1.5 操作的一部分涉及对张量的缩减。我已经实现了简单的交错缩减算法,如所述here .但是,似乎并没有减少整个缓冲区。 block 减少的
我是一名优秀的程序员,十分优秀!