gpt4 book ai didi

algorithm - 如何分割二进制字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:17:48 25 4
gpt4 key购买 nike

我有一个问题,这是我博士工作的一部分。
我有一个表示像素的24 bits序列最左边的8 bits表示像素的red分量,中间的8 bits表示green分量,最右边的8 bits表示blue分量。像素的红色、绿色和蓝色分量可以分别取0(二进制00000000)到255(二进制11111111)之间的值。
我需要确定这个像素的颜色是几何上最接近纯红色,绿色,白色还是黑色。rgb分量(r1, g1, b1)(r2,g2,b2)的两种颜色之间的几何距离由

d = ( (r1-r2)^2 + (g1-g2)^2 + (b1-b2)^2 ) ^ 1/2

纯黑的rgb值 (0, 0, 0),纯白的rgb值 (255, 255, 255),纯红的rgb值 (255, 0, 0),纯蓝的rgb值 (0, 0, 255)
我的任务是确定这五种颜色中哪种最接近像素的颜色。
例如,如果像素由 P表示,则
红色分量= 000000001111111100000110(二进制)=0(基数10)
绿色分量= 00000000(二进制)=255(基数10)
蓝色分量= 11111111(二进制)=6(基数10)
所以,p的rgb值是(0,255,6)
Euclidean Distance of P from pure black (RGB = (0,0,0)):  
d = ( (0 - 0)^2 + (255 - 0)^2 + (6 - 0)2))^1/2 = 65061^1/2

Euclidean Distance of P from pure white (RGB = (255, 255, 255)):
d = ( (0 - 255)^2 + (255 - 255)^2 + (6 - 255)2))^1/2 = 127026^1/2

Euclidean Distance of P from pure red (RGB = (255, 0, 0)):
d = ( (0 - 255)^2 + (255 - 0)^2 + (6 - 0)^2))^1/2 = 130086^1/2

Euclidean Distance of P from pure green (RGB = (0, 255, 0)):
d = ( (0 - 0)^2 + (255 - 255)^2 + (6 - 0)^2))^1/2 = 36^1/2

Euclidean Distance of P from pure blue (RGB = (0, 0, 255)):
d = ( (0 - 0)^2 + (255 - 0)^2 + (6 - 255)^2))^1/2 = 127026^1/2

从上面我们看到,p最接近纯绿色。
如果P与两种或更多颜色等距,则应输出“digulary”
样本输入:
5 // The first line contains an integer N(1<=N<=100), which is the number of input pixels to follow.
101111010110011011100100
110000010101011111101111
100110101100111111101101
010111011010010110000011
000000001111111111111111

样本输出:
White
White
White
Green
Ambiguous

最佳答案

下面是一个实时JavaScript实现:

function Color(num, name) {
this.red = (num>>16) & 0xFF;
this.green = (num>> 8) & 0xFF;
this.blue = num & 0xFF;
this.name = name;
this.square_dist = function (rgb) {
return Math.pow(rgb.red - this.red, 2)
+ Math.pow(rgb.green - this.green, 2)
+ Math.pow(rgb.blue - this.blue, 2);
};
}

var pures = [
new Color(0x000000, 'black'),
new Color(0xFFFFFF, 'white'),
new Color(0xFF0000, 'red'),
new Color(0x00FF00, 'green'),
new Color(0x0000FF, 'blue'),
];

function findNearestColour(num) {
var rgb = new Color(num),
name, square, result,
smallest_square = 0x40000,
count = 0;
for (pure of pures) {
square = pure.square_dist(rgb);
if (square <= smallest_square) {
if (square < smallest_square) {
result = pure.name;
smallest_square = square;
count = 0;
}
count++;
}
}
return count == 1 ? result : 'ambiguous';
}


// I/O

var input = document.querySelector('input');
var output = document.querySelector('pre');

input.oninput = function() {
// Get input
var value = input.value;

// Process
var result = findNearestColour(parseInt(value.trim(), 2));

// Output result
output.textContent = result;
}
input.oninput();

<input value="101111010110011011100100" size="30"><br>
<pre></pre>

关于algorithm - 如何分割二进制字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com