-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from notV3NOM/development
Development
- Loading branch information
Showing
9 changed files
with
692 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { | ||
convertToBlackAndWhite, | ||
edgeDetection, | ||
imageDataToCanvas, | ||
} from '@genshin-optimizer/img-util' | ||
|
||
interface Point { | ||
x: number | ||
y: number | ||
} | ||
interface Rectangle { | ||
topLeft: Point | ||
bottomRight: Point | ||
} | ||
type artifactPredictorResult = { | ||
artifactImageData: ImageData | ||
debugImgs: Record<string, string> | ||
} | ||
|
||
export function artifactBoxPredictor( | ||
imageData: ImageData | ||
): artifactPredictorResult { | ||
const debugImgs = {} as Record<string, string> | ||
|
||
imageData = | ||
imageData.width > imageData.height | ||
? boxPredictor(imageData, debugImgs) | ||
: imageData | ||
|
||
return { | ||
artifactImageData: imageData, | ||
debugImgs: debugImgs, | ||
} | ||
} | ||
|
||
function boxPredictor( | ||
imageData: ImageData, | ||
debugImgs: Record<string, string> | ||
): ImageData { | ||
const edgeDetectedImageData = edgeDetection(imageData) | ||
|
||
debugImgs['Edge Detection Full Screen'] = imageDataToCanvas( | ||
edgeDetectedImageData | ||
).toDataURL() | ||
|
||
const bwEdgeData = convertToBlackAndWhite( | ||
new ImageData( | ||
new Uint8ClampedArray(edgeDetectedImageData.data), | ||
edgeDetectedImageData.width, | ||
edgeDetectedImageData.height | ||
) | ||
) | ||
|
||
const { topLeft, bottomRight } = findLargestRectangle(bwEdgeData) | ||
|
||
const canvas = document.createElement('canvas') | ||
const context = canvas.getContext('2d')! | ||
const width = bottomRight.x - topLeft.x + 1 | ||
const height = bottomRight.y - topLeft.y + 1 | ||
|
||
canvas.width = width | ||
canvas.height = height | ||
|
||
context.drawImage( | ||
imageDataToCanvas(imageData), | ||
topLeft.x, | ||
topLeft.y, | ||
width, | ||
height, | ||
0, | ||
0, | ||
width, | ||
height | ||
) | ||
|
||
const largestRectangleImageData = context.getImageData(0, 0, width, height) | ||
debugImgs['Largest Rectangle'] = canvas.toDataURL() | ||
|
||
return largestRectangleImageData | ||
} | ||
|
||
function findLargestRectangle(imageData: ImageData): Rectangle { | ||
const width = imageData.width | ||
const height = imageData.height | ||
const data = imageData.data | ||
|
||
let maxArea = 0 | ||
let maxRectangle: Rectangle = { | ||
topLeft: { x: 0, y: 0 }, | ||
bottomRight: { x: 0, y: 0 }, | ||
} | ||
|
||
function isWhite(x: number, y: number): boolean { | ||
const pixelIndex = (y * width + x) * 4 | ||
return data[pixelIndex] === 255 | ||
} | ||
|
||
function expandFromPoint(x: number, y: number): Rectangle { | ||
let left = x | ||
let right = x | ||
let top = y | ||
let bottom = y | ||
|
||
while (left > 0 && isWhite(left - 1, y)) left-- | ||
while (right < width - 1 && isWhite(right + 1, y)) right++ | ||
while (top > 0 && isWhite(x, top - 1)) top-- | ||
while (bottom < height - 1 && isWhite(x, bottom + 1)) bottom++ | ||
|
||
return { | ||
topLeft: { x: left, y: top }, | ||
bottomRight: { x: right, y: bottom }, | ||
} | ||
} | ||
|
||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
if (isWhite(x, y)) { | ||
const rectangle = expandFromPoint(x, y) | ||
const area = | ||
(rectangle.bottomRight.x - rectangle.topLeft.x + 1) * | ||
(rectangle.bottomRight.y - rectangle.topLeft.y + 1) | ||
if (area > maxArea) { | ||
maxArea = area | ||
maxRectangle = rectangle | ||
} | ||
} | ||
} | ||
} | ||
|
||
return maxRectangle | ||
} |
Oops, something went wrong.