gpt4 book ai didi

c++ - 照片拼接算法。如何在给定基本图像和瓷砖列表的情况下创建马赛克照片?

转载 作者:可可西里 更新时间:2023-11-01 17:10:57 25 4
gpt4 key购买 nike

Hy.What I have to do is to create a program (using C or C++), takes as input a 24bits/pixel bitmap and a gathering of images 我必须创建一个马赛克图像,类似于输入图像使用给定的图像库(创建类似于输入的马赛克照片)。

到目前为止,我可以访问输入的图像像素及其颜色,但我有点卡住了。我的问题是我应该从哪里开始?我需要一个可以做这样的事情的基本算法。而且我真的找不到任何东西(也许我看错了)。还有人可以告诉我一个随机的照片下载器,以便我可以为该项目下载小图像吗?有人能帮我吗?请告诉我从哪里开始以及使用什么。

最佳答案

我已经在 Scala 中完成了这个。 Dr Dobbs article对我非常有用。

示例图片:

Sample photomosaic

这是我的基本算法:

def createMosaic(targetImage:BufferedImage,
index:PhotoIndexer.PhotoIndex,
opacity:Float,
targetWidth:Int,
targetHeight:Int,
numRows:Int,
numColumns:Int, callback:PhotoMosaicCallback): ImageGrid = {

var indexCopy = index

// Map from the buffered image to that image's average color
var colorMap:Map[BufferedImage,Color] =
index.values.map(data => (data.thumbnail, data.avgColor)).toMap

// We look at rectangular regions of the target image, calculate their average
// colors, and then pick images that match those colors.
val sampleWidth = targetImage.getWidth / numColumns
val sampleHeight = targetImage.getHeight / numRows

// Used to report the progress of the process
var counter = 1
val numSubImages = numRows * numColumns

val imageGrid:ImageGrid = Array.fill(numRows, numColumns)(Nil)

// for each patch in the image
for (row <- 0 until numRows) {
for (column <- 0 until numColumns) {
val x = column * sampleWidth
val y = row * sampleHeight
// This is the small rectangular region of the target image that we're
// currently considering
val subImage = targetImage.getData(new Rectangle(x,y,sampleWidth,sampleHeight))
val avgImageColor = calculateColorFromRaster(subImage)

val nearest:Seq[BufferedImage] = getNearestColorImages(avgImageColor, colorMap)

// nearest is in sorted order; pick one of them and draw it to correct place in
// image
imageGrid(row)(column) = nearest

callback.photosCalculated(row, column, nearest)

val percent = 100.0 * counter / numSubImages
// TODO: for GUI version, use a display bar
if (counter % 100 == 0) {
println(percent + " completed (" + counter + " of" + numSubImages + ")")
}
counter+=1
}
}
imageGrid
}

My full sourcecode is available在知乎上

关于c++ - 照片拼接算法。如何在给定基本图像和瓷砖列表的情况下创建马赛克照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5478519/

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