简体   繁体   English

我如何在 nrrd 文件中将灰色转换为 RGB

[英]How do i convert Gray to RGB in nrrd file

i trying convert grayscale 3D image to RGB 3D image.我尝试将灰度 3D 图像转换为 RGB 3D 图像。

Now i can get each slice's array.现在我可以得到每个切片的数组。 this array value's grayscale pixel value.此数组值的灰度像素值。

but i don't know how to covert RGB value.但我不知道如何转换 RGB 值。

i tried convert color using opencv function.我尝试使用 opencv 函数转换颜色。

import numpy as np
import nrrd
import cv2

data = nrrd.read('C:\\Users\\admin\\Desktop\\sample data\\sample_nrrd.nrrd')
print(data[0][442][376])
cv2.cvtColor(data,cv2.COLOR_GRAY2BGR)

but it's not working...但它不起作用......

i first time using nrrd file.我第一次使用 nrrd 文件。

how to convert gray to rgb.如何将灰色转换为 rgb。

And this array is example of my data.这个数组是我的数据的例子。

Thanks.谢谢。

[-1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 80 236 1830 1901 1852 1742 1430 1147 1088 1285 1240 989 969 787 791 1073 1098 1380 1320 1125 1075 1209 1433 1505 1349 1114 1261 1463 1454 1696 1435 1301 1448 1384 1146 1220 1054 829 1189 1245 1319 1293 986 695 672 594 709 583 503 601 562 440 418 764 967 1275 911 842 761 652 479 691 715 505 442 768 650 705 938 1079 1076 969 936 907 902 755 588 614 770 738 646 971 802 625 890 1020 929 941 824 800 803 920 843 793 834 937 877 737 494 621 605 763 825 642 548 527 427 552 529 572 345 442 455 603 614 712 521 603 687 770 665 744 604 642 791 971 980 1059 1020 842 781 793 845 860 982 916 1077 907 491 806 533 327 709 817 913 977 735 958 624 547 651 952 1171 1184 1033 1262 2015 2193 2444 2830 2678 2650 2473 2528 2766 2915 2991 2654 2403 2700 2646 2302 2276 2706 3003 2639 2499 2414 1948 1456 1908 1409 852 500 946 747 715 864 899 960 977 807 954 1348 1053 1242 1346 1732 1634 1600 1690 1730 1797 1833 [-1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 80 236 1830 1901 1852 1742 1430 1147 1088 1285 1240 989 969 787 791 1073 1098 1380 1320 1125 1075 1209 1433 1505 1349 1114 1261 1463 1454 1696 1435 1301 1448 1384 1146 1220 1054 829 1189 1245 1319 1293 986 695 672 594 709 583 503 601 562 440 418 764 967 1275 911 842 761 652 479 691 715 505 442 768 650 705 938 1079 1076 969 936 907 902 755 588 614 770 738 646 971 802 625 890 1020 929 941 824 800 803 920 843 793 834 937 877 737 494 621 605 763 825 642 548 527 427 552 529 572 345 442 455 603 614 712 521 603 687 770 665 744 604 642 791 971 980 1059 1020 842 781 793 845 860 982 916 1077 907 491 806 533 327 709 817 913 977 735 958 624 547 651 952 1171 1184 1033 1262 2015 2193 2444 2830 2678 2650 2473 2528 2766 2915 2991 2654 2403 2700 2646 2302 2276 2706 3003 2639 2499 2414 1948 1456 1908 1409 852 500 946 747 715 864 899 960 977 807 954 1348 1053 1242 1346 1732 1634 1600 1690 1730 1797 1833 1963 1795 1775 2016 2182 2260 2132 1912 1651 1380 1576 1768 2275 1934 1790 1740 1908 2061 2068 1879 1714 1801 1678 1588 1669 1717 1596 1573 2080 1869 1922 2080 1701 2003 1617 1917 1810 1437 1292 1110 813 1079 1166 1037 1111 1518 1417 1037 603 120 137 15 -30 -197 -409 -133 -72 80 7 10 -7 -28 29 -219 -12 3 18 144 120 -89 -4 101 143 66 -162 96 218 153 120 36 188 275 58 -64 28 9 -77 89 202 206 243 349 234 54 163 262 313 282 131 175 234 102 263 109 93 57 143 282 235 175 189 217 200 297 345 314 150 -24 105 111 202 -58 20 -67 -175 -39 271 292 -2 -153 -181 -41 200 67 104 128 91 -154 -171 -42 -125 67 -172 -101 -59 -130 -94 -146 -175 23 -51 230 104 91 -16 -75 -169 -246 -203 -90 45 -99 11 72 287 149 57 111 79 -12 -104 206 0 41 68 78 -65 -255 -136 -115 53 52 61 -30 119 -155 -229 -190 -36 -163 -240 98 84 85 -17 1 54 81 -173 -205 -172 -351 -19 -86 -172 -98 -90 -169 257 126 83 171 284 297 159 50 -150 -94 -45 -39 -12 230 201 215 328 144 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -100 1963 1795 1775 2016 2182 2260 2132 1912 1651 1380 1576 1768 2275 1934 1790 1740 1908 2061 2068 1879 1714 1801 1678 1588 1669 1717 1596 1573 2080 1869 1922 2080 1701 2003 1617 1917 1810 1437 1292 1110 813 1079 1166 1037 1111 1518 1417 1037 603 120 137 15 -30 -197 -409 -133 -72 80 7 10 -7 -28 29 -219 -12 3 18 144 120 -89 -4 101 143 66 -162 96 218 153 15 6 8 2 8 77 89 202 206 243 349 234 54 163 262 313 282 131 175 234 102 263 109 93 57 143 282 235 175 189 217 200 297 345 314 150 -24 105 111 202 -58 -67 20 -39 -175 271 292 -2 - 153 -181 -41 200 67 104 128 91 -154 -171 -42 -125 67 -172 -101 -59 -130 -94 -146 -175 23 -51 230 104 91 -16 -725 -126 90 45 -99 11 72 287 149 57 111 79 -12 -104 206 0 41 68 78 -65 -255 -136 -115 53 52 61 -30 119 -155 -223 -19 -19 -19 8 17 1 54 81 -173 -205 -172 -351 -19 -86 -172 -98 -90 -169 257 126 83 171 284 297 159 50 -150 -94 -45 -39 -12 12 230 10 1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -100 0 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000] 0 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -0010 -001001 1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000 -1000]

Your question is not very precise and accurate and there can be multiple understanding of it.你的问题不是很准确准确,可以有多种理解。

First of all there is no actual 1D color to 3D color conversion, the COLOR_GRAY2BGR only convert 1-channel grey to 3-channel grey.首先没有实际的1D颜色到3D颜色的转换,COLOR_GRAY2BGR只将1通道灰度转换为3通道灰度。 This conversion does not add any color, just change internal image representation so we can save it in the video .此转换不会添加任何颜色,只是更改内部图像表示,以便我们可以将其保存在视频中。 The color can not recover.颜色无法恢复。 Since it is a 3D depth map, I think what you want is color mapping function like the image below.既然是 3D 深度图,我想你想要的是像下图这样的颜色映射功能。

在此处输入图片说明

Not sure your data is the final disparity or final depth.不确定您的数据是最终视差还是最终深度。 So you have to figur out it yourself later.所以以后得自己想办法。 But the general idea is to contain it within cv::Mat and the use opencv color mapping function to colorize.但总体思路是将其包含在 cv::Mat 中,并使用 opencv 颜色映射函数进行着色。

Assume data_image_1D is np array假设 data_image_1D 是 np 数组

import numpy as np 
import cv2
data_image_1D # this is the np array that you got it from somewhere
bw_img = np.reshape(data_image_1D,(rows,cols))
#rows and cols are the size of the depth image that you have. Try to see if you can get this convetsion working. 
im_color = cv2.applyColorMap(bw_img, cv2.COLORMAP_JET)
cv2.imshow("im_color",im_color)
cv2.imshow("bw_img",bw_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

But still, you have to deal with the negative disparity, minimal disparity and maximum disparity issue.但是,您仍然必须处理负差异、最小差异和最大差异问题。 That one I can leave it to you那个我可以留给你的

For more data mapping or custom color mapping you can follow this guide https://www.learnopencv.com/applycolormap-for-pseudocoloring-in-opencv-c-python/有关更多数据映射或自定义颜色映射,您可以按照本指南https://www.learnopencv.com/applycolormap-for-pseudocoloring-in-opencv-c-python/

edit编辑

I think what you want might be just a simple merge function.我认为您想要的可能只是一个简单的合并功能。 See this看到这个

import numpy as np
import nrrd
######you orginal code put it here###
bw_img = np.reshape(data_image_1D,(rows,cols))
im_color = cv2.merge([bw_img, bw_img, bw_img])
nrrd.write(filename, im_color)# Write to a NRRD file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM