我正在使用FreeMat,我有一张RGB图片,这是一张3D矩阵,包含图片的列和行以及每个像素的RGB值。
由于没有将RGB图片转换为YIQ的内在函数,我实现了一个。我想出了这段代码:
假设我有一个3D数组,image_rgb
:
matrix = [0.299 0.587 0.114;
0.596 -0.274 -0.322;
0.211 -0.523 0.312];
row = 1:length(image_rgb(:,1,1));
col = 1:length(image_rgb(1,:,1));
p = image_rgb(row,col,:);
%Here I have the problem
mage_yiq(row,col,:) = matrix*image_rgb(row,col,:);
max_y = max (max(image_yiq(:,:,1)));
max_i = max (max(image_yiq(:,:,2)));
max_q = max (max(image_yiq(:,:,3)));
%Renormalize the image again after the multipication
% to [0,1].
image_yiq(:,:,1) = image_yiq(:,:,1)/max_y;
image_yiq(:,:,2) = image_yiq(:,:,2)/max_i;
image_yiq(:,:,3) = image_yiq(:,:,3)/max_q;
我无法理解为什么矩阵乘法失败。我希望代码很好而不仅仅是,手工繁殖矩阵......
答案 0 :(得分:2)
您尝试使用您创建的matrix
多个3D数组,这不是正确的矩阵乘法。您应该将图像数据展开为3×m * n矩阵,并将其与自定义矩阵相乘。
以下是将自定义色彩空间转换应用于RGB图像的解决方案。我使用了您提供的矩阵,并将其与内置的YIQ变换进行了比较。
%# Define the conversion matrix
matrix = [0.299 0.587 0.114;
0.596 -0.274 -0.322;
0.211 -0.523 0.312];
%# Read your image here
rgb = im2double(imread('peppers.png'));
subplot(1,3,1), imshow(rgb)
title('RGB')
%# Convert using unfolding and folding
[m n k] = size(rgb);
%# Unfold the 3D array to 3-by-m*n matrix
A = permute(rgb, [3 1 2]);
A = reshape(A, [k m*n]);
%# Apply the transform
yiq = matrix * A;
%# Ensure the bounds
yiq(yiq > 1) = 1;
yiq(yiq < 0) = 0;
%# Fold the matrix to a 3D array
yiq = reshape(yiq, [k m n]);
yiq = permute(yiq, [2 3 1]);
subplot(1,3,2), imshow(yiq)
title('YIQ (with custom matrix)')
%# Convert using the rgb2ntsc method
yiq2 = rgb2ntsc(rgb);
subplot(1,3,3), imshow(yiq2)
title('YIQ (built-in)')
请注意,k
对于RGB图像为3。在每个语句后查看矩阵的大小。不要忘记将图片转换为double
。
答案 1 :(得分:1)
使用相同的矩阵和-color-matrix函数可以使用Imagemagick执行此操作:
输入:
--Start Script
sendToAHK = function (key)
--print('It was assigned string: ' .. key)
local file = io.open("C:\\Users\\TaranWORK\\Documents\\GitHub\\2nd-keyboard-master\\LUAMACROS\\keypressed.txt", "w") -- writing this string to a text file on disk is probably NOT the best method. Feel free to program something better!
--Make sure to substitute the path that leads to your own "keypressed.txt" file, using the double backslashes.
--print("we are inside the text file")
file:write(key)
file:flush() --"flush" means "save"
file:close()
lmc_send_keys('{F24}') -- This presses F24. Using the F24 key to trigger AutoHotKey is probably NOT the best method. Feel free to program something better!
end
local config = { -- this is line 85
[45] = "insert",
[36] = "home",
[33] = "pageup",
[46] = "delete",
[35] = "end",
[34] = "pagedown",
[27] = "escape",
[112] = "F1",
[113] = "F2",
[114] = "F3",
[115] = "F4",
[116] = "F5",
[117] = "F6",
[118] = "F7",
[119] = "F8",
[120] = "F9",
[121] = "F10",
[122] = "F11",
[123] = "F12",
[8] = "backspace",
[220] = "backslash",
[13] = "enter",
[16] = "rShift",
[17] = "rCtrl",
[38] = "up",
[37] = "left",
[40] = "down",
[39] = "right",
[32] = "space",
[186] = "semicolon",
[222] = "singlequote",
[190] = "period",
[191] = "slash",
[188] = "comma",
[219] = "leftbracket",
[221] = "rightbracket",
[189] = "minus",
[187] = "equals",
[96] = "num0",
[97] = "num1",
[98] = "num2",
[99] = "num3",
[100] = "num4",
[101] = "num5",
[102] = "num6",
[103] = "num7",
[104] = "num8",
[105] = "num9",
[106] = "numMult",
[107] = "numPlus",
[108] = "numEnter" --sometimes this is different, check your keyboard
[109] = "numMinus",
[110] = "numDelete",
[111] = "numDiv",
[144] = "numLock", --probably it is best to avoid this key. I keep numlock ON, or it has unexpected effects
[192] = "`", --this is the tilde key just before the number row
[9] = "tab",
[20] = "capslock",
[18] = "alt",
[string.byte('Q')] = "q",
[string.byte('W')] = "w",
[string.byte('E')] = "e",
[string.byte('R')] = "r",
[string.byte('T')] = "t",
[string.byte('Y')] = "y",
[string.byte('U')] = "u",
[string.byte('I')] = "i",
[string.byte('O')] = "o",
[string.byte('P')] = "p",
[string.byte('A')] = "a",
[string.byte('S')] = "s",
[string.byte('D')] = "d",
[string.byte('F')] = "f",
[string.byte('G')] = "g",
[string.byte('H')] = "h",
[string.byte('J')] = "j",
[string.byte('K')] = "k",
[string.byte('L')] = "l",
[string.byte('Z')] = "z",
[string.byte('X')] = "x",
[string.byte('C')] = "c",
[string.byte('V')] = "v",
[string.byte('B')] = "b",
[string.byte('N')] = "n",
[string.byte('M')] = "m",
[string.byte('0')] = "0",
[string.byte('1')] = "1",
[string.byte('2')] = "2",
[string.byte('3')] = "3",
[string.byte('4')] = "4",
[string.byte('5')] = "5",
[string.byte('6')] = "6",
[string.byte('7')] = "7",
[string.byte('8')] = "8",
[string.byte('9')] = "9",
--[255] = "printscreen" --these keys do not work
}
-- define callback for whole device
lmc_set_handler('MACROS', function(button, direction)
--Ignoring upstrokes ensures keystrokes are not registered twice, but activates faster than ignoring downstrokes. It also allows press and hold behaviour
if (direction == 0) then return end -- ignore key upstrokes.
if type(config[button]) == "string" then
print(' ')
print('Your key ID number is: ' .. button)
print('It was assigned string: ' .. config[button])
sendToAHK(config[button])
else
print(' ')
print('Not yet assigned: ' .. button)
end
end)
但这不是真正的sRGB转YIQ。
这是一个sRGB到YIQ转换,显示YIQ就好像RGB:
convert peppers_tiny.png -color-matrix \
" \
0.299 0.587 0.114 \
0.596 -0.274 -0.322 \
0.211 -0.523 0.312 \
" \
peppers_tiny_yiq.png
这是相同的,但交换前两个频道:
convert peppers_tiny.png -colorspace YIQ -separate \
-set colorspace sRGB -combine peppers_tiny_yiq2.png