图像处理建议 – 将uint32数组转换为图像

huangapple go评论80阅读模式
英文:

Image processing advise - converting uint32 array to Image

问题

我正在学习图像处理,并选择使用Go作为开始处理图像的首选语言。

我有一个uint32值的数组,例如:

[[287 277 283 297 295 279 279 292 305 295 285 282 290 297 293 290 285 282 279 282 297 300 300 297 290 290 290 293 306 310 303 293 300 295 288 288 292 292 295 311 301 295 285 288 298 298 288 292 288 295 306 303 297 293 306 313 313 310 303 303 306 310 305 305 303 303 300 297 290 293 303 313 295 292 298 305 301 301 301 295 310 300 297 300 300 300 306 316 303 306 306 310 310 306 297 290 301 305 311 308 301 295 292 292 295 295 298 298 298 298 298 298 306 303 303 297 293 297 303 310 303 300 300 303 300 293 297 303 303 303 303 300 300 297 300 300 297 300 303 310 310 310 306 303 305 301 301 301 301 298 298 298 301 301 301 298 298 301 301 295 298 301 298 295 298 301 298 295 292 288 285 282 288 295 301 308 298 292 292 292 295 298 292 288 288 292 288 285 282 285 285 282 290 290 297 300 293 283 283 290 290 293 297 293 287 280 277 274 283 287 287 283 283 283 287 290 290 287 287 283 280 287 290 280 287 290 293 287 283 283 282 275 288 288 287 283 277 277 277 280 277 280 283 280 277 270 270 274 287 280 277 277 280 280 280 280 277 277 274 274 274 274 277 277 270 274 277 280 283 283 280 277 279 282 280 275 277 295 315 310 306 321 341 432 620 754 768 751 660 478 357 326 316 313 305 301 293 297 297 292 290 290 293 295 282 277 277 280 280 280 277 274 287 293 297 287 280 283 287 290 297 300 303 303 300 297 300 303 287 297 300 293 293 300 303 301 303 306 306 297 288 295 298 298 292 295 293 297 297 297 300 300 305 305 305 305 305 301 301 301 308 305 298 292 295 301 301 295 293 290 293 300 300 300 300 300 310 300 303 306 303 300 306 306 313 310 306 306 306 306 301 301 305 305 305 305 305 305 303 300 311 311 308 305 305 308 305 301 288 298 288 292 298 301 315 301 295 301 305 298 301 308 305 292 305 298 292 292 298 301 301 301 298 298 298 295 301 305 298 285 292 295 295 295 295 298 292 285 285 288 288 292 292 288 287 283 287 283 280 283 290 293 297 297 293 290 287 283 280 280 283 287 280 283 283 280 274 267 270 274 264 280 290 280 267 261 274 277 277 277 274 270 270 274 272 265 254 261 270 277 277 270 264 257 256 259 265 265 259 252 246 243 246 249 249 246 246 249 251 247 244 244 244 241 234 231 228 228 231 238 234 225 218 218 221 221 212 212 212 212 210 210 210 210 210 203 207 213 203 197 197 197 184 180 184 187 187 180 177 177 167 171 174 177 174 171 161 158 149 153 156 149 143 136 140 143 136 136 140 143 140 133 130 133 130 123 120 123 130 130 117 107 128 126 122 122 115 112 113 117], [], ... []]

我认为这个问题更多地涉及将这个数组转换为图像的步骤。

我假设会涉及一些位运算和转换等操作,但是否有人知道任何具体的算法,甚至是Go的核心库,或者一些我可以参考的材料来实现这种转换?

我相信一定有相关的资源存在,但我在没有确定和自信的情况下很难引用它们。

如果有人能指点我正确的方向,我将非常感激...

我有关于传感器类型的以下信息,如果有助于理解像素值...

SensorType是Monochrome、RGGB、CMYG、CMYG2或LRGB *或* Color
英文:

I'm just learning about image processing, and have chosen go as my preferred language to begin working with images.

I have an array of uint32 values, such as:

[[287 277 283 297 295 279 279 292 305 295 285 282 290 297 293 290 285 282 279 282 297 300 300 297 290 290 290 293 306 310 303 293 300 295 288 288 292 292 295 311 301 295 285 288 298 298 288 292 288 295 306 303 297 293 306 313 313 310 303 303 306 310 305 305 303 303 300 297 290 293 303 313 295 292 298 305 301 301 301 295 310 300 297 300 300 300 306 316 303 306 306 310 310 306 297 290 301 305 311 308 301 295 292 292 295 295 298 298 298 298 298 298 306 303 303 297 293 297 303 310 303 300 300 303 300 293 297 303 303 303 303 300 300 297 300 300 297 300 303 310 310 310 306 303 305 301 301 301 301 298 298 298 301 301 301 298 298 301 301 295 298 301 298 295 298 301 298 295 292 288 285 282 288 295 301 308 298 292 292 292 295 298 292 288 288 292 288 285 282 285 285 282 290 290 297 300 293 283 283 290 290 293 297 293 287 280 277 274 283 287 287 283 283 283 287 290 290 287 287 283 280 287 290 280 287 290 293 287 283 283 282 275 288 288 287 283 277 277 277 280 277 280 283 280 277 270 270 274 287 280 277 277 280 280 280 280 277 277 274 274 274 274 277 277 270 274 277 280 283 283 280 277 279 282 280 275 277 295 315 310 306 321 341 432 620 754 768 751 660 478 357 326 316 313 305 301 293 297 297 292 290 290 293 295 282 277 277 280 280 280 277 274 287 293 297 287 280 283 287 290 297 300 303 303 300 297 300 303 287 297 300 293 293 300 303 301 303 306 306 297 288 295 298 298 292 295 293 297 297 297 300 300 305 305 305 305 305 301 301 301 308 305 298 292 295 301 301 295 293 290 293 300 300 300 300 300 310 300 303 306 303 300 306 306 313 310 306 306 306 306 301 301 305 305 305 305 305 305 303 300 311 311 308 305 305 308 305 301 288 298 288 292 298 301 315 301 295 301 305 298 301 308 305 292 305 298 292 292 298 301 301 301 298 298 298 295 301 305 298 285 292 295 295 295 295 298 292 285 285 288 288 292 292 288 287 283 287 283 280 283 290 293 297 297 293 290 287 283 280 280 283 287 280 283 283 280 274 267 270 274 264 280 290 280 267 261 274 277 277 277 274 270 270 274 272 265 254 261 270 277 277 270 264 257 256 259 265 265 259 252 246 243 246 249 249 246 246 249 251 247 244 244 244 241 234 231 228 228 231 238 234 225 218 218 221 221 212 212 212 212 210 210 210 210 210 203 207 213 203 197 197 197 184 180 184 187 187 180 177 177 167 171 174 177 174 171 161 158 149 153 156 149 143 136 140 143 136 136 140 143 140 133 130 133 130 123 120 123 130 130 117 107 128 126 122 122 115 112 113 117], [], ... []]

I'm thinking of this problem as more what steps should I take to take this array and be able to convert it to an image?

I'm assuming there will be a number of bitwise operations, and convertions etc, but does anyone know of any specific algorithms perhaps even a core Go library, or perhaps some materials I can reference to be able to achieve this conversion?

I'm sure something exists, but I'm struggling to reference it without surety and confidence.

I'd be eternally grateful if someone can point me in the right direction ...

I have the following information on the sensor type, if that is useful, for understanding pixel values...

SensorType is Monochrome, RGGB, CMYG, CMYG2 or LRGB *or* Color

答案1

得分: 2

这是一个尺寸为600x800的灰度图像。如果你通过运行wc命令来统计单词数:

wc YOURFILE.TXT
0  480000 1015388 YOURFILE.TXT

你会发现它有:

  • 没有换行符(全部在一行),
  • 480,000个单词(或数字组),
  • 大约一百万个字符。

其中的480,000是一个线索,表明它是一个600x800像素的灰度图像,因为如果是3通道的RGB图像,它应该是600x800x3,如果是4通道的RGBA图像,它应该是600x800x4。

你现在可以使用Go来提取它,但是这是我是如何做的...

首先,写一个简单的NetPBM PGM头部,表示它是灰度图像,ASCII格式(非二进制),尺寸为600x800,最大值为255。然后,通过将[]替换为换行符(tr表示“翻译/转置”)来去除所有的[],生成一个PGM文件:

{ printf "P2\n600 800\n255\n"; tr '[]' '\n\n' < YOURFILE.TXT ; } > image.pgm

现在,image.pgm的内容如下:

P2
600 800
255
7 7 7 8 8 7
...
...

请注意,PGM文件可以在GIMPfehxvPhotoshop、macOS的Preview.app以及很可能在IrfanView甚至MS Paint中轻松查看。

然后,我使用ImageMagick自动调整对比度并生成JPEG图像:

magick image.pgm -auto-level image.jpg

如你所见,auto-level对于这种低对比度的图像来说有点过于严厉,并且产生了条纹效果,但你可以以其他不那么严厉的方式进行对比度拉伸。

英文:

It is a greyscale image with dimensions 600x800. If you run your file through wc to count the words:

wc YOURFILE.TXT
0  480000 1015388 YOURFILE.TXT

you will see it has:

  • no newlines (all one line),
  • 480,000 words (or groups of digits) and
  • around a million characters.

The 480,000 is a clue that is 600x800 pixels, and greyscale because it would have 600x800x3 if 3-channel RGB and 600x800x4 if 4-channel RGBA.

You can probably extract it now with Go, but here is how I did it...

First write a simple NetPBM PGM header to say it is greyscale, ASCII (not binary) and 600x800 with 255 as the maximum value. Then remove all the [] by replacing (tr means "translate/transpose") them with linefeeds to make a PGM file:

{ printf &quot;P2\n600 800\n255\n&quot;; tr &#39;[]&#39; &#39;\n\n&#39; &lt; YOURFILE.TXT ; } &gt; image.pgm

image.pgm now looks like this:

P2
600 800
255
7 7 7 8 8 7
...
...

Note that PGM files can be readily viewed in GIMP, feh, xv, Photoshop, macOS Preview.app and most likely in IrfanView and maybe even MS Paint.

Then I used ImageMagick to auto-level the contrast and make a JPEG:

magick image.pgm -auto-level image.jpg

图像处理建议 – 将uint32数组转换为图像

As you can see, auto-level is a bit harsh for such a low contrast image and it has created banding effects, but you can contrast stretch in other, less harsh, ways.

huangapple
  • 本文由 发表于 2022年10月3日 18:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/73934178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定