Making the diamond square fractal algorithm infinite - python
I'm trying to generate an infinite map, as such. I'm doing this in Python, and I can't get the noise libraries to correctly work (they don't seem to ever find my VS2010, and doing it in raw Python would be way too slow). As such, I'm trying to use the Diamond-Square Algorithm.
Would it be possible, in some way, to make this technically infinite?
If not, should I just go back to attempting to get one of the Python noise bindings to work?
This can be done. Divide up your landscape into "tiles", and apply the midpoint displacement algorithm (as I prefer to call it) within each tile.
Each tile must be big enough so that the height at one corner does not significantly depend on the height in another. In that way, you can create and destroy tiles on the fly, setting the height at newly appearing corners to an independent random value.
That value (and the randomness within the tile as well) must be seeded from the tile's position, so that you get the same tile each time.
The latest version of the noise module (at http://pypi.python.org/pypi/noise/1.0b3) mentions that it "fixed problems compiling with Visual C++ on Windows" - you might want to try it again. It installs and works properly for me (Python 2.7.1, MinGW, Windows 7).
If you arrange your tiles in an x,y grid and seed the random number generator for each tile like random.seed((x,y)), then every time you return to the same patch of world it will re-create the same terrain.
With the diamond-square algorithm, two sides of each tile depend on the adjoining tiles; however the dependence does not propagate all the way across the tile. If you let each tile depend on the tiles preceding it (ie above and to the left) and write a create_fake_tile function which assumes that all the preceding-tile values are 0, you get a "bad" tile in which the right column and bottom row are the correct values on which the next tile depends. If you draw each screen starting with the tile row and column preceding the first row and column on screen, then the visible portion of your world will be correct.
Can you apply midpoint displacement from the bottom up? Say you're working on a discrete grid, and you want to generate an MxN region. Get yourself a weighting function w(i). Start by applying a random displacement with weight w(0) to each point. Then apply a random displacement with weight w(1) to every other point, and propagate the displacement to intervening points. Then do every fourth point with w(2), again propagating. You can now generate a grid of any size, without having any starting assumption about the size.
If there's some N for which w(i > N) = 0, then you can stop generating when you hit it - which is rather important if you want the generation to ever terminate! You probably want a w function that starts at 1, increases to some value, and then falls off to zero; the increasing bit models the power-law roughness of terrain up to 100-km or so scales, and the decreasing bit models the fact that whole planets are more or less spherical. If you were doing Mars rather than Earth, you'd want w(i) to go further, because of the Tharsis bulge.
Now replace the random function with a random-looking but deterministic function of the point coordinates (feed the coordinates into a SHA1 hash, for example). Now, whenever you generate some particular part of the grid, it will look the same.
You should be able to do diamond-square in much the same way as this; you just need to alternate the shape of the displacements.
I solved the Diamond Squared algorithm for infinite landscapes.
function diamondSquaredMap(x, y, width, height, iterations) {
var map = fieldDiamondSquared(x, y, x+width, y+height, iterations);
var maxdeviation = getMaxDeviation(iterations);
for (var j = 0; j < width; j++) {
for (var k = 0; k < height; k++) {
map[j][k] = map[j][k] / maxdeviation;
map[j][k] = (map[j][k] + 1) / 2;
}
}
return map;
function create2DArray(d1, d2) {
var x = new Array(d1),
i = 0,
j = 0;
for (i = 0; i < d1; i += 1) {
x[i] = new Array(d2);
}
return x;
}
function fieldDiamondSquared(x0, y0, x1, y1, iterations) {
if (x1 < x0) { return null; }
if (y1 < y0) { return null; }
var finalwidth = x1 - x0;
var finalheight = y1 - y0;
var finalmap = create2DArray(finalwidth, finalheight);
if (iterations === 0) {
for (var j = 0; j < finalwidth; j++) {
for (var k = 0; k < finalheight; k++) {
finalmap[j][k] = displace(iterations,x0+j,y0+k) ;
}
}
return finalmap;
}
var ux0 = Math.floor(x0 / 2) - 1;
var uy0 = Math.floor(y0 / 2) - 1;
var ux1 = Math.ceil(x1 / 2) + 1;
var uy1 = Math.ceil(y1 / 2) + 1;
var uppermap = fieldDiamondSquared(ux0, uy0, ux1, uy1, iterations-1);
var uw = ux1 - ux0;
var uh = uy1 - uy0;
var cx0 = ux0 * 2;
var cy0 = uy0 * 2;
var cw = uw*2-1;
var ch = uh*2-1;
var currentmap = create2DArray(cw,ch);
for (var j = 0; j < uw; j++) {
for (var k = 0; k < uh; k++) {
currentmap[j*2][k*2] = uppermap[j][k];
}
}
var xoff = x0 - cx0;
var yoff = y0 - cy0;
for (var j = 1; j < cw-1; j += 2) {
for (var k = 1; k < ch-1; k += 2) {
currentmap[j][k] = ((currentmap[j - 1][k - 1] + currentmap[j - 1][k + 1] + currentmap[j + 1][k - 1] + currentmap[j + 1][k + 1]) / 4) + displace(iterations,cx0+j,cy0+k);
}
}
for (var j = 1; j < cw-1; j += 2) {
for (var k = 2; k < ch-1; k += 2) {
currentmap[j][k] = ((currentmap[j - 1][k] + currentmap[j + 1][k] + currentmap[j][k - 1] + currentmap[j][k + 1]) / 4) + displace(iterations,cx0+j,cy0+k);
}
}
for (var j = 2; j < cw-1; j += 2) {
for (var k = 1; k < ch-1; k += 2) {
currentmap[j][k] = ((currentmap[j - 1][k] + currentmap[j + 1][k] + currentmap[j][k - 1] + currentmap[j][k + 1]) / 4) + displace(iterations,cx0+j,cy0+k);
}
}
for (var j = 0; j < finalwidth; j++) {
for (var k = 0; k < finalheight; k++) {
finalmap[j][k] = currentmap[j+xoff][k+yoff];
}
}
return finalmap;
}
// Random function to offset
function displace(iterations, x, y) {
return (((PRH(iterations,x,y) - 0.5)*2)) / (iterations+1);
}
function getMaxDeviation(iterations) {
var dev = 0.5 / (iterations+1);
if (iterations <= 0) return dev;
return getMaxDeviation(iterations-1) + dev;
}
//This function returns the same result for given values but should be somewhat random.
function PRH(iterations,x,y) {
var hash;
x &= 0xFFF;
y &= 0xFFF;
iterations &= 0xFF;
hash = (iterations << 24);
hash |= (y << 12);
hash |= x;
var rem = hash & 3;
var h = hash;
switch (rem) {
case 3:
hash += h;
hash ^= hash << 32;
hash ^= h << 36;
hash += hash >> 22;
break;
case 2:
hash += h;
hash ^= hash << 22;
hash += hash >> 34;
break;
case 1:
hash += h;
hash ^= hash << 20;
hash += hash >> 2;
}
hash ^= hash << 6;
hash += hash >> 10;
hash ^= hash << 8;
hash += hash >> 34;
hash ^= hash << 50;
hash += hash >> 12;
return (hash & 0xFFFF) / 0xFFFF;
}
};
//CANVAS CONTROL
window.onload = terrainGeneration;
function terrainGeneration() {
"use strict";
var mapDimension,
roughness,
iterations,
mapCanvas = document.getElementById('canvas');
var update = document.getElementById('update');
var xpos = 0;
var ypos = 0;
mapDimension = 512;
mapCanvas.width = mapDimension;
mapCanvas.height = mapDimension;
var updatefunction = function() {
var elIterations = document.getElementById('iterations');
iterations = parseInt(elIterations.value, 10);
iterations = iterations || 6;
MoveMap(10,0);
}
update.onclick = updatefunction;
updatefunction();
function MoveMap(dx, dy) {
xpos -= dx;
ypos -= dy;
var map = diamondSquaredMap(xpos, ypos, mapDimension, mapDimension, iterations);
drawMap(mapDimension, "canvas", map);
}
var m = this;
m.map = document.getElementById("canvas");
m.width = mapDimension;
m.height = mapDimension;
m.hoverCursor = "auto";
m.dragCursor = "url(data:image/vnd.microsoft.icon;base64,AAACAAEAICACAAcABQAwAQAAFgAAACgAAAAgAAAAQAAAAAEAAQAAAAAAAAEAAAAAAAAAAAAAAgAAAAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD8AAAA/AAAAfwAAAP+AAAH/gAAB/8AAAH/AAAB/wAAA/0AAANsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA//////////////////////////////////////////////////////////////////////////////////////gH///4B///8Af//+AD///AA///wAH//+AB///wAf//4AH//+AD///yT/////////////////////////////8=), default";
m.scrollTime = 300;
m.mousePosition = new Coordinate;
m.mouseLocations = [];
m.velocity = new Coordinate;
m.mouseDown = false;
m.timerId = -1;
m.timerCount = 0;
m.viewingBox = document.createElement("div");
m.viewingBox.style.cursor = m.hoverCursor;
m.map.parentNode.replaceChild(m.viewingBox, m.map);
m.viewingBox.appendChild(m.map);
m.viewingBox.style.overflow = "hidden";
m.viewingBox.style.width = m.width + "px";
m.viewingBox.style.height = m.height + "px";
m.viewingBox.style.position = "relative";
m.map.style.position = "absolute";
function drawMap(size, canvasId, mapData) {
var canvas = document.getElementById(canvasId),
ctx = canvas.getContext("2d"),
x = 0,
y = 0,
colorFill,
img = ctx.createImageData(canvas.height, canvas.width);
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
colorFill = {r: 0, g: 0, b: 0};
var standardShade = Math.floor(mapData[x][y] * 250);
colorFill = {r: standardShade, g: standardShade, b: standardShade};
var pData = (x + (y * canvas.width)) * 4;
img.data[pData] = colorFill.r;
img.data[pData + 1] = colorFill.g;
img.data[pData + 2] = colorFill.b;
img.data[pData + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
function AddListener(element, event, f) {
if (element.attachEvent) {
element["e" + event + f] = f;
element[event + f] = function() {
element["e" + event + f](window.event)
};
element.attachEvent("on" + event, element[event + f])
} else
element.addEventListener(event, f, false)
}
function Coordinate(startX, startY) {
this.x = startX;
this.y = startY;
}
var MouseMove = function(b) {
var e = b.clientX - m.mousePosition.x;
var d = b.clientY - m.mousePosition.y;
MoveMap(e, d);
m.mousePosition.x = b.clientX;
m.mousePosition.y = b.clientY
};
/**
* mousedown event handler
*/
AddListener(m.viewingBox, "mousedown", function(e) {
m.viewingBox.style.cursor = m.dragCursor;
// Save the current mouse position so we can later find how far the
// mouse has moved in order to scroll that distance
m.mousePosition.x = e.clientX;
m.mousePosition.y = e.clientY;
// Start paying attention to when the mouse moves
AddListener(document, "mousemove", MouseMove);
m.mouseDown = true;
event.preventDefault ? event.preventDefault() : event.returnValue = false;
});
/**
* mouseup event handler
*/
AddListener(document, "mouseup", function() {
if (m.mouseDown) {
var handler = MouseMove;
if (document.detachEvent) {
document.detachEvent("onmousemove", document["mousemove" + handler]);
document["mousemove" + handler] = null;
} else {
document.removeEventListener("mousemove", handler, false);
}
m.mouseDown = false;
if (m.mouseLocations.length > 0) {
var clickCount = m.mouseLocations.length;
m.velocity.x = (m.mouseLocations[clickCount - 1].x - m.mouseLocations[0].x) / clickCount;
m.velocity.y = (m.mouseLocations[clickCount - 1].y - m.mouseLocations[0].y) / clickCount;
m.mouseLocations.length = 0;
}
}
m.viewingBox.style.cursor = m.hoverCursor;
});
}
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Canvas Terrain Generator</title>
<link rel="stylesheet" href="terrain.css" type="text/css">
<style type="text/css"></style><style type="text/css"></style></head>
<body>
Click and Pan.<br>
<div style="cursor: auto; overflow: hidden; width: 512px; height: 512px; position: relative;"><canvas id="canvas" width="512" height="512" style="position: absolute;"></canvas></div>
<br>
<form>
<fieldset>
<legend>Height Map Properties</legend>
<ol class="options">
<li>
<input type="text" name="iterations" id="iterations">
<label for="iterations">
Iterations(6)
</label>
</li>
</ol>
</fieldset>
<input type="button" value="Update" id="update">
</form>
</body></html>
https://jsfiddle.net/Tatarize/1Lrj3s2v/3/
Click and Drag jsfiddle
And actually found this question doing my due diligence. Yes. Though the answers provided here are mostly wrong it totally can be done. Conceptually what you need to do is view each iteration of the algorithm as an infinite field and the base case as an infinite field of perfectly random numbers. So each iteration is a diamond squared version of the previous one.
And to solve a range of say tile [100-200][100-200] at iteration 7, what you need is the entire block half that size (~a quarter that size h*w) at iteration 6 plus one extra edge on each side. So if you have a function that solves for a given tile:
getTerrain(x0,y0,x1,y1,iteration)... you can solve this for that tile if you have [49,101][49,101] at iteration 6. Then you simply apply diamond squared everywhere you can which won't work at the edges but will work everywhere else, and provide you with exactly enough data to solve for the tile [100-200][100-200] at iteration 7.
To get that tile at iteration 6 it will request [23-52][23-52] from iteration 5. And this will continue until iteration 0 just gives you [-1,3][-1,3] which will be 16 perfectly random values. If you requested a tile so large that iteration 0 didn't yet reach that size and position, you'll just need a different sliver which is fine because you're just making them up anyway.
In this way you get rid of the seeding step completely, simplify the algorithm, make it infinite, make it take a reasonable memory footprint. Using a deterministic pseudorandom number hashed from the coords and it will let you generate and regenerate the identical tiles on the fly, because you actually generated the upper lower iterations and just did so in a focused manner.
My blog has more information on it,
http://godsnotwheregodsnot.blogspot.com/2013/11/field-diamond-squared-fractal-terrain.html
In reality most of the complication of the algorithm comes from having the base case four points in a loop, as this is not close to the simplest case. And it's apparently escaped most people that you could have even simplified that by doing 1 point in a loop (though this is still pointlessly complex). Or anything that makes the algorithm seem to have at least 4x4 points. You could even start with 4x4 points iterate once, drop any row or column with an unknown value (all the edges), then have a 5x5 block, then a 7x7 block then an 11x11 block... (2n-3) x (2n-3).
In short, if you have an infinite field for your base case, you can actually have an infinite field, the iterations just determine how blended your stuff is. And if you use something deterministic for the pseudorandom injected offset, you pretty much have a very quick infinite landscape generator.
and here's a demonstration of it in javascript,
http://tatarize.nfshost.com/FieldDiamondSquare.htm
Here's an implementation in javascript. It simply gives you a view of the correct field at that iteration, position, and size. The above linked example uses this code in a movable canvas. It stores no data and recalculates each frame.
function diamondSquaredMap(x, y, width, height, iterations) {
var map = fieldDiamondSquared(x, y, x+width, y+height, iterations);
var maxdeviation = getMaxDeviation(iterations);
for (var j = 0; j < width; j++) {
for (var k = 0; k < height; k++) {
map[j][k] = map[j][k] / maxdeviation;
}
}
return map;
function create2DArray(d1, d2) {
var x = new Array(d1),
i = 0,
j = 0;
for (i = 0; i < d1; i += 1) {
x[i] = new Array(d2);
}
return x;
}
function fieldDiamondSquared(x0, y0, x1, y1, iterations) {
if (x1 < x0) { return null; }
if (y1 < y0) { return null; }
var finalwidth = x1 - x0;
var finalheight = y1 - y0;
var finalmap = create2DArray(finalwidth, finalheight);
if (iterations === 0) {
for (var j = 0; j < finalwidth; j++) {
for (var k = 0; k < finalheight; k++) {
finalmap[j][k] = displace(iterations,x0+j,y0+k) ;
}
}
return finalmap;
}
var ux0 = Math.floor(x0 / 2) - 1;
var uy0 = Math.floor(y0 / 2) - 1;
var ux1 = Math.ceil(x1 / 2) + 1;
var uy1 = Math.ceil(y1 / 2) + 1;
var uppermap = fieldDiamondSquared(ux0, uy0, ux1, uy1, iterations-1);
var uw = ux1 - ux0;
var uh = uy1 - uy0;
var cx0 = ux0 * 2;
var cy0 = uy0 * 2;
var cw = uw*2-1;
var ch = uh*2-1;
var currentmap = create2DArray(cw,ch);
for (var j = 0; j < uw; j++) {
for (var k = 0; k < uh; k++) {
currentmap[j*2][k*2] = uppermap[j][k];
}
}
var xoff = x0 - cx0;
var yoff = y0 - cy0;
for (var j = 1; j < cw-1; j += 2) {
for (var k = 1; k < ch-1; k += 2) {
currentmap[j][k] = ((currentmap[j - 1][k - 1] + currentmap[j - 1][k + 1] + currentmap[j + 1][k - 1] + currentmap[j + 1][k + 1]) / 4) + displace(iterations,cx0+j,cy0+k);
}
}
for (var j = 1; j < cw-1; j += 2) {
for (var k = 2; k < ch-1; k += 2) {
currentmap[j][k] = ((currentmap[j - 1][k] + currentmap[j + 1][k] + currentmap[j][k - 1] + currentmap[j][k + 1]) / 4) + displace(iterations,cx0+j,cy0+k);
}
}
for (var j = 2; j < cw-1; j += 2) {
for (var k = 1; k < ch-1; k += 2) {
currentmap[j][k] = ((currentmap[j - 1][k] + currentmap[j + 1][k] + currentmap[j][k - 1] + currentmap[j][k + 1]) / 4) + displace(iterations,cx0+j,cy0+k);
}
}
for (var j = 0; j < finalwidth; j++) {
for (var k = 0; k < finalheight; k++) {
finalmap[j][k] = currentmap[j+xoff][k+yoff];
}
}
return finalmap;
}
// Random function to offset
function displace(iterations, x, y) {
return (((PRH(iterations,x,y) - 0.5)*2)) / (iterations+1);
}
function getMaxDeviation(iterations) {
var dev = 0.5 / (iterations+1);
if (iterations <= 0) return dev;
return getMaxDeviation(iterations-1) + dev;
}
//This function returns the same result for given values but should be somewhat random.
function PRH(iterations,x,y) {
var hash;
x &= 0xFFF;
y &= 0xFFF;
iterations &= 0xFF;
hash = (iterations << 24);
hash |= (y << 12);
hash |= x;
var rem = hash & 3;
var h = hash;
switch (rem) {
case 3:
hash += h;
hash ^= hash << 32;
hash ^= h << 36;
hash += hash >> 22;
break;
case 2:
hash += h;
hash ^= hash << 22;
hash += hash >> 34;
break;
case 1:
hash += h;
hash ^= hash << 20;
hash += hash >> 2;
}
hash ^= hash << 6;
hash += hash >> 10;
hash ^= hash << 8;
hash += hash >> 34;
hash ^= hash << 50;
hash += hash >> 12;
return (hash & 0xFFFF) / 0xFFFF;
}
};
Updated to add, I went on from this, and made my own noise algorithm based on the same scoped field algorithm here, Here's the Jsfiddle for it.
https://jsfiddle.net/rkdzau7o/
You can click and drag the noise around.
Related
How to pass a python variable into an HTML widget object in Google Colab
I am using a Google Colab notebook. I have a p5 HTML object in the form of a widget that takes in a color palette and a quote then generates an animation. If I hardcode the palette and quote into the HTML object it works just fine, but as soon as I try to pass it in through variables it does not work anymore. I am wondering if there is a specific way I have to pass them in to the HTML widget? Any help or direction will be greatly appreciated! Here is the code I have: def CreateAnimation(p,q): animation = widgets.HTML(''' <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/addons/p5.sound.min.js"></script> <script> new p5(); let numBalls = 40; let spring = 0.1; let gravity = 0.0001; let friction = -0.9; let balls = []; let paletteList = p; let phrase = q; function setup() { createCanvas(600, 500); for (let i = 0; i < numBalls; i++) { balls[i] = new Ball( random(width), random(height), random(30, 110), i, balls, color(random(paletteList)) ); } noStroke(); fill(255, 204); } function draw() { let x = map(mouseX, 0, width, 0, 255, true) background(x); balls.forEach(ball => { ball.collide(); ball.move(); ball.display(); }); push(); fill(map(mouseX, 0, width, 255, 0, true)); textSize(40); text(phrase, (width/2 - width/2.5),(height/2 - height/4.5), height, width); pop(); } class Ball { constructor(xin, yin, din, idin, oin, hexin) { this.x = xin; this.y = yin; this.vx = 0; this.vy = 0; this.diameter = din; this.id = idin; this.others = oin; this.hex= hexin; } collide() { for (let i = this.id + 1; i < numBalls; i++) { let dx = this.others[i].x - this.x; let dy = this.others[i].y - this.y; let distance = sqrt(dx * dx + dy * dy); let minDist = this.others[i].diameter / 2 + this.diameter / 2; if (distance < minDist) { //console.log("2"); let angle = atan2(dy, dx); let targetX = this.x + cos(angle) * minDist; let targetY = this.y + sin(angle) * minDist; let ax = (targetX - this.others[i].x) * spring; let ay = (targetY - this.others[i].y) * spring; this.vx -= ax; this.vy -= ay; this.others[i].vx += ax; this.others[i].vy += ay; } } } move() { this.vy += gravity; this.x += this.vx; this.y += this.vy; if (this.x + this.diameter / 2 > width) { this.x = width - this.diameter / 2; this.vx *= friction; } else if (this.x - this.diameter / 2 < 0) { this.x = this.diameter / 2; this.vx *= friction; } if (this.y + this.diameter / 2 > height) { this.y = height - this.diameter / 2; this.vy *= friction; } else if (this.y - this.diameter / 2 < 0) { this.y = this.diameter / 2; this.vy *= friction; } } display() { fill(this.hex); ellipse(this.x, this.y, this.diameter, this.diameter); } } </script> ''') return animation #Then call it with something like this: a=CreateAnimation(['#381A38','#DD9B80','#E6937A','#B18079','#8B636C'],'It is a good day to have a good day')
PySide2 Qt Surface Example
I would like to reimplement the Qt C++ "Surface" example (Q3DSurface) in PySide2 but QSurfaceDataArray and QSurfaceDataRow are not available. void SurfaceGraph::fillSqrtSinProxy() { float stepX = (sampleMax - sampleMin) / float(sampleCountX - 1); float stepZ = (sampleMax - sampleMin) / float(sampleCountZ - 1); QSurfaceDataArray *dataArray = new QSurfaceDataArray; dataArray->reserve(sampleCountZ); for (int i = 0 ; i < sampleCountZ ; i++) { QSurfaceDataRow *newRow = new QSurfaceDataRow(sampleCountX); // Keep values within range bounds, since just adding step can cause minor drift due // to the rounding errors. float z = qMin(sampleMax, (i * stepZ + sampleMin)); int index = 0; for (int j = 0; j < sampleCountX; j++) { float x = qMin(sampleMax, (j * stepX + sampleMin)); float R = qSqrt(z * z + x * x) + 0.01f; float y = (qSin(R) / R + 0.24f) * 1.61f; (*newRow)[index++].setPosition(QVector3D(x, y, z)); } *dataArray << newRow; } m_sqrtSinProxy->resetArray(dataArray); } Is there are way to use a QVector<QSurfaceDataItem> in PySide2? from PySide2.QtDataVisualization import QtDataVisualization as QDV data_item = QDV.QSurfaceDataItem() data_item.setPosition(QVector3D(x, y, z)) The QSurfaceDataItem is available but I can't pass the objects to QSurfaceDataProxy without QVector.
Detecting boxes via Hough Transform
Using HoughTransform I am trying to detect boxes and provide for a distinct color. So far my understanding is a box is horizontal and vertical line . my code is lines = cv2.HoughLines(edges,1,np.pi/180, 50) # The below for loop runs till r and theta values # are in the range of the 2d array for r,theta in lines[0]: # Stores the value of cos(theta) in a a = np.cos(theta) # Stores the value of sin(theta) in b b = np.sin(theta) # x0 stores the value rcos(theta) x0 = a*r # y0 stores the value rsin(theta) y0 = b*r # x1 stores the rounded off value of (rcos(theta)-1000sin(theta)) x1 = int(x0 + 1000*(-b)) # y1 stores the rounded off value of (rsin(theta)+1000cos(theta)) y1 = int(y0 + 1000*(a)) # x2 stores the rounded off value of (rcos(theta)+1000sin(theta)) x2 = int(x0 - 1000*(-b)) # y2 stores the rounded off value of (rsin(theta)-1000cos(theta)) y2 = int(y0 - 1000*(a)) # cv2.line draws a line in img from the point(x1,y1) to (x2,y2). # (255,255,255) denotes the colour of the line to be. cv2.line(img,(x1,y1), (x2,y2), (255,255,255),2) ` What could i do so that the boxes can be colored or identified?
You should do vertical and horizontal line detection separately so that you can make them each more specific. Go through all your lines and compile a list of intersections between horizontal and vertical line combinations Now you have a list of 2d points that if you draw them should pretty much be on the corners of the boxes. The final step is to collect those points into meaningful sets. To get those sets, I would start with the point nearest origin (just for the sake of starting somewhere). I would look through all the other points for the closest other point that has a greater x but is withing +-5 (or some configurable range) y of the starting point. Then do the same but in the y direction. You now have the bottom corner of the box. Which you could just complete and start your ocr on, but to be more robust, find the final corner as well. Once all 4 corners are found, remove all of those points from your intersection array and add however you want to denote box locations into a new array. Rinse and repeat as now a different point will be nearest origin. Without actually testing this, I think it will choke (or need some conditional improvement for missing walls) on the K box but should be pretty generic to variable box shapes and sizes. Edit 1: In testing, I am finding that it will probably be difficult to separate the close corners of two adjacent boxes. I think a more generic and robust solution would be to after you get collisions, do a point clustering operation at about 1/3 box min side length. This will average corners together with their nearest neighbor. So this will slightly change the strategy as you will need to use every corner twice (box to left and box to right) except for end points. Wrote up some test code and it is functional, here are the outputs: Code, sorry for c++ and not at all optimized, happy friday :) //CPP libaries #include <stdio.h> #include <mutex> #include <thread> //Included libraries //Note: these headers have to be before any opencv due to a namespace collision (could probably be fixed) #include <opencv2/opencv.hpp> #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace cv; // Finds the intersection of two lines, or returns false. // The lines are defined by (o1, p1) and (o2, p2). //https://stackoverflow.com/questions/7446126/opencv-2d-line-intersection-helper-function bool intersection(Point2f o1, Point2f p1, Point2f o2, Point2f p2, Point2f &r) { Point2f x = o2 - o1; Point2f d1 = p1 - o1; Point2f d2 = p2 - o2; float cross = d1.x*d2.y - d1.y*d2.x; if (abs(cross) < /*EPS*/1e-8) return false; double t1 = (x.x * d2.y - x.y * d2.x) / cross; r = o1 + d1 * t1; return true; } std::vector<Point2f> clusterPts(std::vector<Point2f> inputPts, double clusterRadius_Squared) { std::vector<Point2f> outputPts = std::vector<Point2f>(); while(inputPts.size()>0) { Point2f clusterCenter = inputPts[0]; while (true) { Point2f newClustCenter = Point2f(0, 0); int averagingCount = 0; std::vector<int> clusterIndicies = std::vector<int>(); for (int i = 0; i < inputPts.size(); i++) { if (clusterRadius_Squared >= pow(inputPts[i].x - clusterCenter.x, 2) + pow(inputPts[i].y - clusterCenter.y, 2)) { newClustCenter.x += inputPts[i].x; newClustCenter.y += inputPts[i].y; averagingCount += 1; clusterIndicies.push_back(i); } } newClustCenter = newClustCenter / (double)averagingCount; if (newClustCenter == clusterCenter) { //remove all points inside cluster from inputPts, stash cluster center, and break inner while loop std::vector<Point2f> remainingPts = std::vector<Point2f>(); for (int i = 0; i < inputPts.size(); i++) { if (std::find(clusterIndicies.begin(), clusterIndicies.end(), i) == clusterIndicies.end()) { remainingPts.push_back(inputPts[i]); } } inputPts = remainingPts; outputPts.push_back(clusterCenter); break; } else { clusterCenter = newClustCenter; } } } return outputPts; } std::vector<Rect> findBoxes(std::vector<Point2f> corners, bool shrinkBoxes = false, int boxSideLength_Guess = 50) { std::vector<Rect> outBoxes = std::vector<Rect>(); int approxBoxSize = 1000 * boxSideLength_Guess; while (corners.size()>4) { //find point above or below (these points will be removed from array after used) int secondPtIndex = -1; for (int i = 1; i < corners.size(); i++) { if (abs(corners[i].x - corners[0].x) < boxSideLength_Guess / 2.0) { secondPtIndex = i; break; } } if (secondPtIndex == -1) { std::cout << "bad box point tossed" << std::endl; corners.erase(corners.begin() + 0); continue; } //now search for closest same level point on either side int thirdIndexRight = -1; int thirdIndexLeft = -1; double minDistRight = approxBoxSize; double minDistLeft = -approxBoxSize; for (int i = 2; i < corners.size(); i++) { if (abs(corners[i].y - corners[secondPtIndex].y) < boxSideLength_Guess / 2.0) { double dist = corners[i].x - corners[secondPtIndex].x; if (dist < 0 && dist > minDistLeft) //check left { minDistLeft = dist; thirdIndexLeft = i; } else if(dist > 0 && dist < minDistRight) //check right { minDistRight = dist; thirdIndexRight = i; } } } if (thirdIndexLeft != -1) { approxBoxSize = 1.5 * abs(minDistLeft); } if (thirdIndexRight != -1) { approxBoxSize = 1.5 * minDistRight; } int fourthIndexRight = -1; int fourthIndexLeft = -1; for (int i = 1; i < corners.size(); i++) { if (i == thirdIndexLeft || i == thirdIndexRight) { continue; } if (thirdIndexLeft != -1 && abs(corners[i].x - corners[thirdIndexLeft].x) < boxSideLength_Guess / 2.0) { fourthIndexLeft = i; } if (thirdIndexRight != -1 && abs(corners[i].x - corners[thirdIndexRight].x) < boxSideLength_Guess / 2.0) { fourthIndexRight = i; } } if (!shrinkBoxes) { if (fourthIndexRight != -1) { outBoxes.push_back(Rect(corners[0], corners[thirdIndexRight])); } if (fourthIndexLeft != -1) { outBoxes.push_back(Rect(corners[0], corners[thirdIndexLeft])); } } else { if (fourthIndexRight != -1) { outBoxes.push_back(Rect(corners[0] * 0.90 + corners[thirdIndexRight] *0.10, corners[0] * 0.10 + corners[thirdIndexRight] * 0.90)); } if (fourthIndexLeft != -1) { outBoxes.push_back(Rect(corners[0] * 0.90 + corners[thirdIndexLeft] * 0.10, corners[0] * 0.10 + corners[thirdIndexLeft] * 0.90)); } } corners.erase(corners.begin() + secondPtIndex); corners.erase(corners.begin() + 0); } std::cout << approxBoxSize << std::endl; return outBoxes; } int main(int argc, char** argv) { Mat image = imread("../../resources/images/boxPic.png", CV_LOAD_IMAGE_GRAYSCALE); imshow("source", image); //namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display. //imshow("Display window", image); // Show our image inside it. Mat edges, lineOverlay, cornerOverlay, finalBoxes; Canny(image, edges, 50, 200, 3); //edges = image; //cvtColor(image, edges, COLOR_GRAY2BGR); cvtColor(image, lineOverlay, COLOR_GRAY2BGR); cvtColor(image, cornerOverlay, COLOR_GRAY2BGR); cvtColor(image, finalBoxes, COLOR_GRAY2BGR); std::cout << image.cols << " , "<<image.rows << std::endl; std::vector<Vec2f> linesHorizontal; std::vector<Point> ptsLH; HoughLines(edges, linesHorizontal, 5, CV_PI / 180, 2 * edges.cols * 0.6, 0.0,0.0, CV_PI / 4, 3 * CV_PI / 4); std::vector<Vec2f> linesVertical; std::vector<Point> ptsLV; HoughLines(edges, linesVertical, 5, CV_PI / 180, 2 * edges.rows * 0.6,0,0,-CV_PI/32,CV_PI/32); for (size_t i = 0; i < linesHorizontal.size(); i++) { float rho = linesHorizontal[i][0], theta = linesHorizontal[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a * rho, y0 = b * rho; pt1.x = cvRound(x0 + 1000 * (-b)); pt1.y = cvRound(y0 + 1000 * (a)); pt2.x = cvRound(x0 - 1000 * (-b)); pt2.y = cvRound(y0 - 1000 * (a)); ptsLH.push_back(pt1); ptsLH.push_back(pt2); line(lineOverlay, pt1, pt2, Scalar(0, 0, 255), 1, LINE_AA); } for (size_t i = 0; i < linesVertical.size(); i++) { float rho = linesVertical[i][0], theta = linesVertical[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a * rho, y0 = b * rho; pt1.x = cvRound(x0 + 1000 * (-b)); pt1.y = cvRound(y0 + 1000 * (a)); pt2.x = cvRound(x0 - 1000 * (-b)); pt2.y = cvRound(y0 - 1000 * (a)); ptsLV.push_back(pt1); ptsLV.push_back(pt2); line(lineOverlay, pt1, pt2, Scalar(0, 255, 0), 1, LINE_AA); } imshow("edged", edges); imshow("detected lines", lineOverlay); //look for collisions std::vector<Point2f> xPts; for (size_t i = 0; i < linesHorizontal.size(); i++) { for (size_t ii = 0; ii < linesVertical.size(); ii++) { Point2f xPt; bool intersectionExists = intersection(ptsLH[2 * i], ptsLH[2 * i + 1], ptsLV[2 * ii], ptsLV[2 * ii + 1], xPt); if (intersectionExists) { xPts.push_back(xPt); } } } waitKey(1000); std::vector<Point2f> boxCorners = clusterPts(xPts, 25*25); for (int i = 0; i < boxCorners.size(); i++) { circle(cornerOverlay, boxCorners[i], 5, Scalar(0, 255, 0), 2); } imshow("detected corners", cornerOverlay); //group make boxes for groups of points std::vector<Rect> ocrBoxes = findBoxes(boxCorners,true); for (int i = 0; i < ocrBoxes.size(); i++) { if (i % 3 == 0) { rectangle(finalBoxes, ocrBoxes[i], Scalar(255, 0, 0), 2); } else if(i % 3 == 1) { rectangle(finalBoxes, ocrBoxes[i], Scalar(0, 255, 0), 2); } else if (i % 3 == 2) { rectangle(finalBoxes, ocrBoxes[i], Scalar(0, 0, 255), 2); } } imshow("detected boxes", finalBoxes); waitKey(0); // Wait for a keystroke in the window return 0; }
Flatten a Two Dimensional array going in a clockwise direction [duplicate]
How do I print a 5×5 two-dimensional array in spiral order? Is there any formula so that I can print an array of any size in spiral order?
The idea is to treat the matrix as a series of layers, top-right layers and bottom-left layers. To print the matrix spirally we can peel layers from these matrix, print the peeled part and recursively call the print on the left over part. The recursion terminates when we don't have any more layers to print. Input matrix: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 After peeling top-right layer: 1 2 3 4 8 5 6 7 2 9 0 1 6 3 4 5 1 7 8 9 After peeling bottom-left layer from sub-matrix: 6 7 5 0 1 9 4 5 3 7 8 9 After peeling top-right layer from sub-matrix: 6 7 1 0 5 4 After peeling bottom-left layer from sub-matrix: 0 4 Recursion terminates. C functions: // function to print the top-right peel of the matrix and // recursively call the print bottom-left on the submatrix. void printTopRight(int a[][COL], int x1, int y1, int x2, int y2) { int i = 0, j = 0; // print values in the row. for(i = x1; i<=x2; i++) { printf("%d ", a[y1][i]); } // print values in the column. for(j = y1 + 1; j <= y2; j++) { printf("%d ", a[j][x2]); } // see if more layers need to be printed. if(x2-x1 > 0) { // if yes recursively call the function to // print the bottom left of the sub matrix. printBottomLeft(a, x1, y1 + 1, x2-1, y2); } } // function to print the bottom-left peel of the matrix and // recursively call the print top-right on the submatrix. void printBottomLeft(int a[][COL], int x1, int y1, int x2, int y2) { int i = 0, j = 0; // print the values in the row in reverse order. for(i = x2; i>=x1; i--) { printf("%d ", a[y2][i]); } // print the values in the col in reverse order. for(j = y2 - 1; j >= y1; j--) { printf("%d ", a[j][x1]); } // see if more layers need to be printed. if(x2-x1 > 0) { // if yes recursively call the function to // print the top right of the sub matrix. printTopRight(a, x1+1, y1, x2, y2-1); } } void printSpiral(int arr[][COL]) { printTopRight(arr,0,0,COL-1,ROW-1); printf("\n"); }
Pop top row Transpose and flip upside-down (same as rotate 90 degrees counter-clockwise) Go to 1 Python 2 code: import itertools arr = [[1,2,3,4], [12,13,14,5], [11,16,15,6], [10,9,8,7]] def transpose_and_yield_top(arr): while arr: yield arr[0] arr = list(reversed(zip(*arr[1:]))) print list(itertools.chain(*transpose_and_yield_top(arr))) For python 3x: import itertools arr = [[1,2,3,4], [12,13,14,5], [11,16,15,6], [10,9,8,7]] def transpose_and_yield_top(arr): while arr: yield arr[0] arr = list(reversed(list(zip(*arr[1:])))) print(list(itertools.chain(*transpose_and_yield_top(arr))))
I see that no one has use only one for loop and without recursion in the code, and so I want to contribute. The idea is like this: Imagine there is a turtle standing at point (0,0), that is, top-left corner, facing east (to the right) It will keep going forward and each time it sees a sign, the turtle will turn right So if we put the turtle at point (0,0) facing right-ward, and if we place the signs at appropriate places, the turtle will traverse the array in spiral way. Now the problem is: "Where to put the signs?" Let's see where we should put the signs (marked by #, and numbers by O): For a grid that looks like this: O O O O O O O O O O O O O O O O We put the signs like this: O O O # # O # O O # # O # O O # For a grid that looks like this: O O O O O O O O O O O O We put the signs like this: O O # # # O O # O # O # And for a grid that looks like this: O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O We put the signs like this: O O O O O O # # O O O O # O O # O O # O O O # O O O # O # O O O O O # We can see that, unless the point is at the top-left part, the signs are places at points where the distances to the closest horizontal border and the closest vertical border are the same, while for the top-left part, the distance to the top border is one more than the distance to the left border, with priority given to top-right in case the point is horizontally centered, and to top-left in case the point is vertically centered. This can be realized in a simple function quite easily, by taking the minimum of (curRow and height-1-curRow), then the minimum of (curCol and width-1-curCol) and compare if they are the same. But we need to account for the upper-left case, that is, when the minimum is curRow and curCol themselves. In that case we reduce the vertical distance accordingly. Here is the C code: #include <stdio.h> int shouldTurn(int row, int col, int height, int width){ int same = 1; if(row > height-1-row) row = height-1-row, same = 0; // Give precedence to top-left over bottom-left if(col >= width-1-col) col = width-1-col, same = 0; // Give precedence to top-right over top-left row -= same; // When the row and col doesn't change, this will reduce row by 1 if(row==col) return 1; return 0; } int directions[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; void printSpiral(int arr[4][4], int height, int width){ int directionIdx=0, i=0; int curRow=0, curCol=0; for(i=0; i<height*width; i++){ printf("%d ",arr[curRow][curCol]); if(shouldTurn(curRow, curCol, height, width)){ directionIdx = (directionIdx+1)%4; } curRow += directions[directionIdx][0]; curCol += directions[directionIdx][1]; } printf("\n"); } int main(){ int arr[4][4]= {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; printSpiral(arr, 4, 4); printSpiral(arr, 3, 4); } Which outputs: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 1 2 3 4 8 12 11 10 9 5 6 7
Here are the three interesting ways Reading in spiral way can be treated like a snake moving towards boundary and turning on hitting the boundary or itself (I find it elegant and most efficient being a single loop of N iterations) ar = [ [ 0, 1, 2, 3, 4], [15, 16, 17, 18, 5], [14, 23, 24, 19, 6], [13, 22, 21, 20, 7], [12, 11, 10, 9, 8]] def print_spiral(ar): """ assuming a rect array """ rows, cols = len(ar), len(ar[0]) r, c = 0, -1 # start here nextturn = stepsx = cols # move so many steps stepsy = rows-1 inc_c, inc_r = 1, 0 # at each step move this much turns = 0 # how many times our snake had turned for i in range(rows*cols): c += inc_c r += inc_r print ar[r][c], if i == nextturn-1: turns += 1 # at each turn reduce how many steps we go next if turns%2==0: nextturn += stepsx stepsy -= 1 else: nextturn += stepsy stepsx -= 1 # change directions inc_c, inc_r = -inc_r, inc_c print_spiral(ar) output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 A recursive approach would be to print outer layer and call same function for inner rectangle e.g. def print_spiral(ar, sr=0, sc=0, er=None, ec=None): er = er or len(ar)-1 ec = ec or len(ar[0])-1 if sr > er or sc > ec: print return # print the outer layer top, bottom, left, right = [], [], [], [] for c in range(sc,ec+1): top.append(ar[sr][c]) if sr != er: bottom.append(ar[er][ec-(c-sc)]) for r in range(sr+1,er): right.append(ar[r][ec]) if ec != sc: left.append(ar[er-(r-sr)][sc]) print " ".join([str(a) for a in top + right + bottom + left]), # peel next layer of onion print_spiral(ar, sr+1, sc+1, er-1, ec-1) Finally here is a small snippet to do it, not efficient but fun :), basically it prints top row, and rotates whole rectangle anti-clockwise and repeats def print_spiral(ar): if not ar: return print " ".join(str(a) for a in ar[0]), ar = zip(*[ reversed(row) for row in ar[1:]]) print_spiral(ar)
This program works for any n*n matrix.. public class circ { public void get_circ_arr (int n,int [][] a) { int z=n; { for (int i=0;i<n;i++) { for (int l=z-1-i;l>=i;l--) { int k=i; System.out.printf("%d",a[k][l]); } for (int j=i+1;j<=z-1-i;j++) { int k=i; { System.out.printf("%d",a[j][k]); } } for (int j=i+1;j<=z-i-1;j++) { int k=z-1-i; { System.out.printf("%d",a[k][j]); } } for (int j=z-2-i;j>=i+1;j--) { int k=z-i-1; { System.out.printf("%d",a[j][k]); } } } } } } Hope it helps
I was obsessed with this problem when I was learning Ruby. This was the best I could do: def spiral(matrix) matrix.empty? ? [] : matrix.shift + spiral(matrix.transpose.reverse) end You can check out some of my other solutions by stepping back through the revisions in this gist. Also, if you follow the link back to whom I forked the gist from, you'll find some other clever solutions. Really interesting problem that can be solved in multiple elegant ways — especially in Ruby.
JavaScript solution: var printSpiral = function (matrix) { var i; var top = 0; var left = 0; var bottom = matrix.length; var right = matrix[0].length; while (top < bottom && left < right) { //print top for (i = left; i < right; i += 1) { console.log(matrix[top][i]); } top++; //print right column for (i = top; i < bottom; i += 1) { console.log(matrix[i][right - 1]); } right--; if (top < bottom) { //print bottom for (i = right - 1; i >= left; i -= 1) { console.log(matrix[bottom - 1][i]); } bottom--; } if (left < right) { //print left column for (i = bottom - 1; i >= top; i -= 1) { console.log(matrix[i][left]); } left++; } } };
One solution involves directions right, left, up, down, and their corresponding limits (indices). Once the first row is printed, and direction changes (from right) to down, the row is discarded by incrementing the upper limit. Once the last column is printed, and direction changes to left, the column is discarded by decrementing the right hand limit... Details can be seen in the self-explanatory C code. #include <stdio.h> #define N_ROWS 5 #define N_COLS 3 void print_spiral(int a[N_ROWS][N_COLS]) { enum {up, down, left, right} direction = right; int up_limit = 0, down_limit = N_ROWS - 1, left_limit = 0, right_limit = N_COLS - 1, downcount = N_ROWS * N_COLS, row = 0, col = 0; while(printf("%d ", a[row][col]) && --downcount) if(direction == right) { if(++col > right_limit) { --col; direction = down; ++up_limit; ++row; } } else if(direction == down) { if(++row > down_limit) { --row; direction = left; --right_limit; --col; } } else if(direction == left) { if(--col < left_limit) { ++col; direction = up; --down_limit; --row; } } else /* direction == up */ if(--row < up_limit) { ++row; direction = right; ++left_limit; ++col; } } void main() { int a[N_ROWS][N_COLS] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; print_spiral(a); } Link for Testing and Download.
Given a matrix of chars, implement a method that prints all characters in the following order: first the outer circle, then the next one and so on. public static void printMatrixInSpiral(int[][] mat){ if(mat.length == 0|| mat[0].length == 0){ /* empty matrix */ return; } StringBuffer str = new StringBuffer(); int counter = mat.length * mat[0].length; int startRow = 0; int endRow = mat.length-1; int startCol = 0; int endCol = mat[0].length-1; boolean moveCol = true; boolean leftToRight = true; boolean upDown = true; while(counter>0){ if(moveCol){ if(leftToRight){ /* printing entire row left to right */ for(int i = startCol; i <= endCol ; i++){ str.append(mat[startRow][i]); counter--; } leftToRight = false; moveCol = false; startRow++; } else{ /* printing entire row right to left */ for(int i = endCol ; i >= startCol ; i--){ str.append(mat[endRow][i]); counter--; } leftToRight = true; moveCol = false; endRow--; } } else { if(upDown){ /* printing column up down */ for(int i = startRow ; i <= endRow ; i++){ str.append(mat[i][endCol]); counter--; } upDown = false; moveCol = true; endCol--; } else { /* printing entire col down up */ for(int i = endRow ; i >= startRow ; i--){ str.append(mat[i][startCol]); counter--; } upDown = true; moveCol = true; startCol++; } } } System.out.println(str.toString()); }
Two dimensional N*N Matrix is Square matrix Idea: We have to traverse in four different directions to traverse like spiral. We have to traverse inside matrix once one layer of spiral is over. So total, we need 5 loops, 4 loops to traverse like spiral and 1 loop to traverse through the layers. public void printSpiralForm(int[][] a, int length) { for( int i = 0 , j = length-1 ; i < j ; i++ , j-- ) { for( int k = i ; k < j ; k++ ) { System.out.print( a[i][k] + " " ) ; } for( int k = i ; k < j ; k++ ) { System.out.print(a[k][j] + " "); } for( int k = j ; k > i ; k-- ) { System.out.print(a[j][k] + " ") ; } for( int k = j ; k > i ; k-- ) { System.out.print( a[k][i] + " " ) ; } } if ( length % 2 == 1 ) { System.out.println( a[ length/2 ][ length/2 ] ) ; } }
Just keep it simple --> public class spiralMatrix { public static void printMatrix(int[][] matrix, int rows, int col) { int rowStart=0; int rowEnd=rows-1; int colStart=0; int colEnd=col-1; while(colStart<=colEnd && rowStart<=rowEnd) { for(int i=colStart;i<colEnd;i++) System.out.println(matrix[rowStart][i]); for(int i=rowStart;i<rowEnd;i++) System.out.println(matrix[i][colEnd]); for(int i=colEnd;i>colStart;i--) System.out.println(matrix[rowEnd][i]); for(int i=rowEnd;i>rowStart;i--) System.out.println(matrix[i][colStart]); rowStart++; colEnd--; rowEnd--; colStart++; } } public static void main(String[] args){ int[][] array={{1,2,3,4},{5,6,7,8}}; printMatrix(array,2,4); } }
This is my implementation: public static void printMatrix(int matrix[][], int M, int N){ int level = 0; int min = (M < N) ? M:N; System.out.println(); while(level <= min/2){ for(int j = level; j < N - level - 1; j++){ System.out.print(matrix[level][j] + "\t"); } for(int i = level; i < M - level - 1; i++) { System.out.print(matrix[i][N - level - 1] + "\t"); } for(int j = N - level - 1; j > level; j--){ System.out.print(matrix[M - level - 1][j] + "\t"); } for(int i = M - level - 1; i > level; i-- ){ System.out.print(matrix[i][level] + "\t"); } level++; } }
Here is my solution. Please correct if I'm wrong. class Spiral: def spiralOrder(self, A): result = [] c = [] c.append(A[0]) b = A[1:] while len(b) > 0: b = self.rotate(b) c.append(b[0]) b = b[1:] for item in c: for fitem in item: print fitem, result.append(fitem) return result def rotate(self,a): b = [] l = zip(*a) for i in xrange(len(l)-1,-1,-1): b.append(list(l[i])) return b if __name__ == '__main__': a = [[1, 2, 3,3], [4, 5, 6,6], [7, 8, 9,10]] s = Spiral() s.spiralOrder(a)
Slash Top Row -> Transpose -> Flip -> Repeat. void slashTransposeFlip(int[][] m){ if( m.length * m[0].length == 1){ //only one element left System.out.print(m[0][0]); }else{ //print the top row for(int a:m[0]){System.out.print(a+" ");} //slash the top row from the matrix. int[][] n = Arrays.copyOfRange(m,1,m.length); int[][] temp = n; int rows = temp.length; int columns = temp[0].length; //invert rows and columns and create new array n = new int[columns][rows]; //transpose for(int x=0;x<rows;x++) for(int y=0;y<columns;y++) n[y][x] = temp[x][y]; //flipping time for (int i = 0; i < n.length / 2; i++) { int[] t = n[i]; n[i] = n[n.length - 1 - i]; n[n.length - 1 - i] = t; } //recursively call again the reduced matrix. slashTransposeFlip(n); } }
Complexity: Single traverse O(n) Please let me add my single loop answer with complexity O(n). I have observed that during left-right and right-left traverse of the matrix, there is an increase and decrease by one respectively in the row-major index. Similarly, for the top-bottom and bottom-top traverse there is increase and decrease by n_cols. Thus I made an algorithm for that. For example, given a (3x5) matrix with entries the row-major indexes the print output is: 1,2,3,4,5,10,15,14,13,12,11,6,7,8,9. ------->(+1) ^ 1 2 3 4 5 | (+n_cols) | 6 7 8 9 10 | (-n_cols) | 11 12 13 14 15 (-1)<------- Code solution: #include <iostream> using namespace std; int main() { // your code goes here bool leftToRight=true, topToBottom=false, rightToLeft=false, bottomToTop=false; int idx=0; int n_rows = 3; int n_cols = 5; int cnt_h = n_cols, cnt_v = n_rows, cnt=0; int iter=1; for (int i=0; i <= n_rows*n_cols + (n_rows - 1)*(n_cols - 1)/2; i++){ iter++; if(leftToRight){ if(cnt >= cnt_h){ cnt_h--; cnt=0; leftToRight = false; topToBottom = true; //cout << "Iter: "<< iter << " break_leftToRight"<<endl; }else{ cnt++; idx++; //cout << "Iter: "<< iter <<" idx: " << idx << " cnt: "<< cnt << " cnt_h: "<< cnt_h<< endl; cout<< idx << endl; } }else if(topToBottom){ if(cnt >= cnt_v-1){ cnt_v--; cnt=0; leftToRight = false; topToBottom = false; rightToLeft=true; //cout << "Iter: "<< iter << " break_topToBottom"<<endl; }else{ cnt++; idx+=n_cols; //cout << "Iter: "<< iter << " idx: " << idx << " cnt: "<< cnt << " cnt_v: "<< cnt_h<< endl; cout << idx <<endl; } }else if(rightToLeft){ if(cnt >= cnt_h){ cnt_h--; cnt=0; leftToRight = false; topToBottom = false; rightToLeft=false; bottomToTop=true; //cout << "Iter: "<< iter << " break_rightToLeft"<<endl; //cout<< idx << endl; }else{ cnt++; idx--; //cout << "Iter: "<< iter << " idx: " << idx << " cnt: "<< cnt << " cnt_h: "<< cnt_h<< endl; cout << idx <<endl; } }else if(bottomToTop){ if(cnt >= cnt_v-1){ cnt_v--; cnt=0; leftToRight = true; topToBottom = false; rightToLeft=false; bottomToTop=false; //cout << "Iter: "<< iter << " break_bottomToTop"<<endl; }else{ cnt++; idx-=n_cols; //cout << "Iter: "<< iter << " idx: " << idx << " cnt: "<< cnt << " cnt_v: "<< cnt_h<< endl; cout<< idx << endl; } } //cout << i << endl; } return 0; }
function spiral(a) { var s = []; while (a.length) { // concat 1st row, push last cols, rotate 180 (reverse inner/outer)... s = s.concat(a.shift()); a = a .map(function(v) { s.push(v.pop()); return v.reverse(); }) .reverse(); } return s; } var arr = [ [1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7] ]; console.log(spiral(arr));// -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] arr = [ [0, 1, 2, 3, 4], [15, 16, 17, 18, 5], [14, 23, 24, 19, 6], [13, 22, 21, 20, 7], [12, 11, 10, 9, 8] ]; console.log(spiral(arr));// -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
For printing a 2-D matrix consider matrix as a composition of rectangles and/or line where smaller rectangle is fitted into larger one, take boundary of matrix which forms a rectangle to be printed, starting with up-left element each time in each layer; once done with this go inside for next layer of smaller rectangle, in case i don't have a rectangle then it should be line to be printed, a horizontal or vertical. I have pasted the code with an example matrix, HTH. #include <stdio.h> int a[2][4] = { 1, 2 ,3, 44, 8, 9 ,4, 55 }; void print(int, int, int, int); int main() { int row1, col1, row2, col2; row1=0; col1=0; row2=1; col2=3; while(row2>=row1 && col2>=col1) { print(row1, col1, row2, col2); row1++; col1++; row2--; col2--; } return 0; } void print(int row1, int col1, int row2, int col2) { int i=row1; int j=col1; /* This is when single horizontal line needs to be printed */ if( row1==row2 && col1!=col2) { for(j=col1; j<=col2; j++) printf("%d ", a[i][j]); return; } /* This is when single vertical line needs to be printed */ if( col1==col2 && row1!=row2) { for(i=row1; j<=row2; i++) printf("%d ", a[i][j]); return; } /* This is reached when there is a rectangle to be printed */ for(j=col1; j<=col2; j++) printf("%d ", a[i][j]); for(j=col2,i=row1+1; i<=row2; i++) printf("%d ", a[i][j]); for(i=row2,j=col2-1; j>=col1; j--) printf("%d ", a[i][j]); for(j=col1,i=row2-1; i>row1; i--) printf("%d ", a[i][j]); }
Here is my implementation in Java: public class SpiralPrint { static void spiral(int a[][],int x,int y){ //If the x and y co-ordinate collide, break off from the function if(x==y) return; int i; //Top-left to top-right for(i=x;i<y;i++) System.out.println(a[x][i]); //Top-right to bottom-right for(i=x+1;i<y;i++) System.out.println(a[i][y-1]); //Bottom-right to bottom-left for(i=y-2;i>=x;i--) System.out.println(a[y-1][i]); //Bottom left to top-left for(i=y-2;i>x;i--) System.out.println(a[i][x]); //Recursively call spiral spiral(a,x+1,y-1); } public static void main(String[] args) { int a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; spiral(a,0,4); /*Might be implemented without the 0 on an afterthought, all arrays will start at 0 anyways. The second parameter will be the dimension of the array*/ } }
//shivi..coding is adictive!! #include<shiviheaders.h> #define R 3 #define C 6 using namespace std; void PrintSpiral(int er,int ec,int arr[R][C]) { int sr=0,sc=0,i=0; while(sr<=er && sc<=ec) { for(int i=sc;i<=ec;++i) cout<<arr[sr][i]<<" "; ++sr; for(int i=sr;i<=er;++i) cout<<arr[i][ec]<<" "; ec--; if(sr<=er) { for(int i=ec;i>=sc;--i) cout<<arr[er][i]<<" "; er--; } if(sc<=ec) { for(int i=er;i>=sr;--i) cout<<arr[i][sc]<<" "; ++sc; } } } int main() { int a[R][C] = { {1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18} }; PrintSpiral(R-1, C-1, a); }
int N = Integer.parseInt(args[0]); // create N-by-N array of integers 1 through N int[][] a = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) a[i][j] = 1 + N*i + j; // spiral for (int i = N-1, j = 0; i > 0; i--, j++) { for (int k = j; k < i; k++) System.out.println(a[j][k]); for (int k = j; k < i; k++) System.out.println(a[k][i]); for (int k = i; k > j; k--) System.out.println(a[i][k]); for (int k = i; k > j; k--) System.out.println(a[k][j]); } // special case for middle element if N is odd if (N % 2 == 1) System.out.println(a[(N-1)/2][(N-1)/2]); } }
Java code if anybody is interested. Input: 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 Output: 1 2 3 4 8 3 7 6 5 4 9 5 6 7 2 1 public class ArraySpiralPrinter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //marrix size //read array int[][] ar = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ar[i][j] = sc.nextInt(); } } printTopRight(0, 0, n - 1, n - 1, ar); } //prints top and right layers. //(x1,y1) to (x1, y2) - top layer & (x1,y2) to (x2, y2) private static void printTopRight(int x1, int y1, int x2, int y2, int[][] ar) { //print row values - top for (int y = y1; y <= y2; y++) { System.out.printf("%d ", ar[x1][y]); } //print column value - right for (int x = x1 + 1; x <= x2; x++) { System.out.printf("%d ", ar[x][y2]); } //are there any remaining layers if (x2 - x1 > 0) { //call printBottemLeft printBottomLeft(x1 + 1, y1, x2, y2 - 1, ar); } } //prints bottom and left layers in reverse order //(x2,y2) to (x2, y1) - bottom layer & (x2,y1) to (x1, y1) private static void printBottomLeft(int x1, int y1, int x2, int y2, int[][] ar) { //print row values in reverse order - bottom for (int y = y2; y >= y1; y--) { System.out.printf("%d ", ar[x2][y]); } //print column value in reverse order - left for (int x = x2-1; x >= x1; x--) { System.out.printf("%d ", ar[x][y1]); } //are there any remaining layers if (x2 - x1 > 0) { printTopRight(x1, y1 + 1, x2 - 1, y2, ar); } } }
This is a recursive version in C that I could think of:- void printspiral (int[][100],int, int, int, int); int main() { int r,c, i, j; printf ("Enter the dimensions of the matrix"); scanf("%d %d", &r, &c); int arr[r][100]; int min = (r<c?r:c); if (min%2 != 0) min = min/2 +1; for (i = 0;i<r; i++) for (j = 0; j<c; j++) scanf ("%d",&arr[i][j]); printspiral(arr,0,r,c,min ); } void printspiral (int arr[][100], int i, int j, int k, int min) { int a; for (a = i; a<k;a++) printf("%d\n", arr[i][a]); for (a=i+1;a<j;a++) printf ("%d\n", arr[a][k-1]); for (a=k-2; a>i-1;a--) printf("%d\n", arr[j-1][a]); for (a=j-2; a>i; a--) printf("%d\n", arr[a][i]); if (i < min) printspiral(arr,i+1, j-1,k-1, min); }
http://www.technicalinterviewquestions.net/2009/03/print-2d-array-matrix-spiral-order.html here is the best explanation for the above answer :) along with diagram :)
public static void printSpiral1(int array[][],int row,int col){ int rowStart=0,colStart=0,rowEnd=row-1,colEnd=col-1; int i; while(rowStart<=rowEnd && colStart<= colEnd){ for(i=colStart;i<=colEnd;i++) System.out.print(" "+array[rowStart][i]); for(i=rowStart+1;i<=rowEnd;i++) System.out.print(" "+array[i][colEnd]); for(i=colEnd-1;i>=colStart;i--) System.out.print(" "+array[rowEnd][i]); for(i=rowEnd-1;i>=rowStart+1;i--) System.out.print(" "+array[i][colStart]); rowStart++; colStart++; rowEnd--; colEnd--; } }
public class SpiralPrint{ //print the elements of matrix in the spiral order. //my idea is to use recursive, for each outer loop public static void printSpiral(int[][] mat, int layer){ int up = layer; int buttom = mat.length - layer - 1; int left = layer; int right = mat[0].length - layer - 1; if(up > buttom+1 || left > right + 1) return; // termination condition //traverse the other frame, //print up for(int i = left; i <= right; i ++){ System.out.print( mat[up][i]+ " " ); } //print right for(int i = up + 1; i <=buttom; i ++){ System.out.print(mat[i][right] + " "); } //print buttom for(int i = right - 1; i >= left; i --){ System.out.print(mat[buttom][i] + " "); } //print left for(int i = buttom - 1; i > up; i --){ System.out.print(mat[i][left] + " "); } //recursive call for the next level printSpiral(mat, layer + 1); } public static void main(String[] args){ int[][] mat = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}}; int[][] mat2 = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}}; SpiralPrint.printSpiral(mat2,0); return; } }
Here is my solution in C#: public static void PrintSpiral(int[][] matrix, int n) { if (matrix == null) { return; } for (int layer = 0; layer < Math.Ceiling(n / 2.0); layer++) { var start = layer; var end = n - layer - 1; var offset = end - 1; Console.Write("Layer " + layer + ": "); // Center case if (start == end) { Console.Write(matrix[start][start]); } // Top for (int i = start; i <= offset; i++) { Console.Write(matrix[start][i] + " "); } // Right for (int i = start; i <= offset; i++) { Console.Write(matrix[i][end] + " "); } // Bottom for (int i = end; i > start; i--) { Console.Write(matrix[end][i] + " "); } // Left for (int i = end; i > start; i--) { Console.Write(matrix[i][start] + " "); } Console.WriteLine(); } }
Here's my approach using an Iterator . Note this solves almost the same problem.. Complete code here : https://github.com/rdsr/algorithms/blob/master/src/jvm/misc/FillMatrix.java import java.util.Iterator; class Pair { final int i; final int j; Pair(int i, int j) { this.i = i; this.j = j; } #Override public String toString() { return "Pair [i=" + i + ", j=" + j + "]"; } } enum Direction { N, E, S, W; } class SpiralIterator implements Iterator<Pair> { private final int r, c; int ri, ci; int cnt; Direction d; // current direction int level; // spiral level; public SpiralIterator(int r, int c) { this.r = r; this.c = c; d = Direction.E; level = 1; } #Override public boolean hasNext() { return cnt < r * c; } #Override public Pair next() { final Pair p = new Pair(ri, ci); switch (d) { case E: if (ci == c - level) { ri += 1; d = changeDirection(d); } else { ci += 1; } break; case S: if (ri == r - level) { ci -= 1; d = changeDirection(d); } else { ri += 1; } break; case W: if (ci == level - 1) { ri -= 1; d = changeDirection(d); } else { ci -= 1; } break; case N: if (ri == level) { ci += 1; level += 1; d = changeDirection(d); } else { ri -= 1; } break; } cnt += 1; return p; } private static Direction changeDirection(Direction d) { switch (d) { case E: return Direction.S; case S: return Direction.W; case W: return Direction.N; case N: return Direction.E; default: throw new IllegalStateException(); } } #Override public void remove() { throw new UnsupportedOperationException(); } } public class FillMatrix { static int[][] fill(int r, int c) { final int[][] m = new int[r][c]; int i = 1; final Iterator<Pair> iter = new SpiralIterator(r, c); while (iter.hasNext()) { final Pair p = iter.next(); m[p.i][p.j] = i; i += 1; } return m; } public static void main(String[] args) { final int r = 19, c = 19; final int[][] m = FillMatrix.fill(r, c); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { System.out.print(m[i][j] + " "); } System.out.println(); } } }
Complete pure C program for any 2D array matrix with given row x column. #include <stdio.h> void printspiral(int *p,int r, int c) { int i=0,j=0,m=1,n=0; static int firstrun=1,gCol; if (!p||r<=0||c<=0) return ; if(firstrun) { gCol=c; firstrun=0; } for(i=0,j=0;(0<=i && i<c)&&(0<=j && j<r);i+=m,j+=n) { printf(" %d",p[i+j*gCol]); if (i==0 && j==1 && (i+1)!=c) break; else if (i+1==c && !j) {m=0;n=1;} else if (i+1==c && j+1==r && j) {n=0;m=-1;} else if (i==0 && j+1==r && j) {m=0;n=-1;} } printspiral(&p[i+j*gCol+1],r-2,c-2); firstrun=1; printf("\n"); } int main() { int a[3][3]={{0,1,2},{3,4,5},{6,7,8}}; int b[3][4]={{0,1,2,3},{4,5,6,7},{8,9,10,11}}; int c[4][3]={{0,1,2},{3,4,5},{6,7,8},{9,10,11}}; int d[3][1]={{0},{1},{2}}; int e[1][3]={{0,1,2}}; int f[1][1]={{0}}; int g[5][5]={{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,14},{15,16,17,18,19},{20,21,22,23,24}}; printspiral(a,3,3); printspiral(b,3,4); printspiral(c,4,3); printspiral(d,3,1); printspiral(e,1,3); printspiral(f,1,1); printspiral(g,5,5); return 0; }
This question is related to this one: Matrix arrangement issues in php The answers presented seem to work but are complicated to understand. A very simple way to solve this is divide and conquer i.e., after reading the edge, remove it and the next read will be much simpler. Check out a complete solution in PHP below: #The source number matrix $source[0] = array(1, 2, 3, 4); $source[1] = array(5, 6, 7, 8); $source[2] = array(9, 10, 11, 12); $source[3] = array(13, 14, 15, 16); $source[4] = array(17, 18, 19, 20); #Get the spiralled numbers $final_spiral_list = get_spiral_form($source); print_r($final_spiral_list); function get_spiral_form($matrix) { #Array to hold the final number list $spiralList = array(); $result = $matrix; while(count($result) > 0) { $resultsFromRead = get_next_number_circle($result, $spiralList); $result = $resultsFromRead['new_source']; $spiralList = $resultsFromRead['read_list']; } return $spiralList; } function get_next_number_circle($matrix, $read) { $unreadMatrix = $matrix; $rowNumber = count($matrix); $colNumber = count($matrix[0]); #Check if the array has one row or column if($rowNumber == 1) $read = array_merge($read, $matrix[0]); if($colNumber == 1) for($i=0; $i<$rowNumber; $i++) array_push($read, $matrix[$i][0]); #Check if array has 2 rows or columns if($rowNumber == 2 || ($rowNumber == 2 && $colNumber == 2)) { $read = array_merge($read, $matrix[0], array_reverse($matrix[1])); } if($colNumber == 2 && $rowNumber != 2) { #First read left to right for the first row $read = array_merge($read, $matrix[0]); #Then read down on right column for($i=1; $i<$rowNumber; $i++) array_push($read, $matrix[$i][1]); #..and up on left column for($i=($rowNumber-1); $i>0; $i--) array_push($read, $matrix[$i][0]); } #If more than 2 rows or columns, pick up all the edge values by spiraling around the matrix if($rowNumber > 2 && $colNumber > 2) { #Move left to right for($i=0; $i<$colNumber; $i++) array_push($read, $matrix[0][$i]); #Move top to bottom for($i=1; $i<$rowNumber; $i++) array_push($read, $matrix[$i][$colNumber-1]); #Move right to left for($i=($colNumber-2); $i>-1; $i--) array_push($read, $matrix[$rowNumber-1][$i]); #Move bottom to top for($i=($rowNumber-2); $i>0; $i--) array_push($read, $matrix[$i][0]); } #Now remove these edge read values to create a new reduced matrix for the next read $unreadMatrix = remove_top_row($unreadMatrix); $unreadMatrix = remove_right_column($unreadMatrix); $unreadMatrix = remove_bottom_row($unreadMatrix); $unreadMatrix = remove_left_column($unreadMatrix); return array('new_source'=>$unreadMatrix, 'read_list'=>$read); } function remove_top_row($matrix) { $removedRow = array_shift($matrix); return $matrix; } function remove_right_column($matrix) { $neededCols = count($matrix[0]) - 1; $finalMatrix = array(); for($i=0; $i<count($matrix); $i++) $finalMatrix[$i] = array_slice($matrix[$i], 0, $neededCols); return $finalMatrix; } function remove_bottom_row($matrix) { unset($matrix[count($matrix)-1]); return $matrix; } function remove_left_column($matrix) { $neededCols = count($matrix[0]) - 1; $finalMatrix = array(); for($i=0; $i<count($matrix); $i++) $finalMatrix[$i] = array_slice($matrix[$i], 1, $neededCols); return $finalMatrix; }
// Program to print a matrix in spiral order #include <stdio.h> int main(void) { // your code goes here int m,n,i,j,k=1,c1,c2,r1,r2;; scanf("%d %d",&m,&n); int a[m][n]; for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } r1=0; r2=m-1; c1=0; c2=n-1; while(k<=m*n) { for(i=c1;i<=c2;i++) { k++; printf("%d ",a[r1][i]); } for(j=r1+1;j<=r2;j++) { k++; printf("%d ",a[j][c2]); } for(i=c2-1;i>=c1;i--) { k++; printf("%d ",a[r2][i]); } for(j=r2-1;j>=r1+1;j--) { k++; printf("%d ",a[j][c1]); } c1++; c2--; r1++; r2--; } return 0; }
OpenCV how to smooth contour, reducing noise
I extracted the contours of an image, that you can see here: However, it has some noise. How can I smooth the noise? I did a close up to make clearer what I want to meant Original image that I've used: Code: rMaskgray = cv2.imread('redmask.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE) (thresh, binRed) = cv2.threshold(rMaskgray, 50, 255, cv2.THRESH_BINARY) Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) r_areas = [cv2.contourArea(c) for c in Rcontours] max_rarea = np.max(r_areas) CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255 for c in Rcontours: if(( cv2.contourArea(c) > max_rarea * 0.70) and (cv2.contourArea(c)< max_rarea)): cv2.drawContours(CntExternalMask,[c],-1,0,1) cv2.imwrite('contour1.jpg', CntExternalMask)
Try an upgrade to OpenCV 3.1.0. After some code adaptations for the new version as shown below, I tried it out with OpenCV version 3.1.0 and did not see any of the effects you are describing. import cv2 import numpy as np print cv2.__version__ rMaskgray = cv2.imread('5evOn.jpg', 0) (thresh, binRed) = cv2.threshold(rMaskgray, 50, 255, cv2.THRESH_BINARY) _, Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) r_areas = [cv2.contourArea(c) for c in Rcontours] max_rarea = np.max(r_areas) CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255 for c in Rcontours: if(( cv2.contourArea(c) > max_rarea * 0.70) and (cv2.contourArea(c)< max_rarea)): cv2.drawContours(CntExternalMask,[c],-1,0,1) cv2.imwrite('contour1.jpg', CntExternalMask)
I don't know if is it ok to provide Java code - but I implemented Gaussian smoothing for openCV contour. Logic and theory is taken from here https://www.morethantechnical.com/2012/12/07/resampling-smoothing-and-interest-points-of-curves-via-css-in-opencv-w-code/ package CurveTools; import org.apache.log4j.Logger; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import java.util.ArrayList; import java.util.List; import static org.opencv.core.CvType.CV_64F; import static org.opencv.imgproc.Imgproc.getGaussianKernel; class CurveSmoother { private double[] g, dg, d2g, gx, dx, d2x; private double gx1, dgx1, d2gx1; public double[] kappa, smoothX, smoothY; public double[] contourX, contourY; /* 1st and 2nd derivative of 1D gaussian */ void getGaussianDerivs(double sigma, int M) { int L = (M - 1) / 2; double sigma_sq = sigma * sigma; double sigma_quad = sigma_sq * sigma_sq; dg = new double[M]; d2g = new double[M]; g = new double[M]; Mat tmpG = getGaussianKernel(M, sigma, CV_64F); for (double i = -L; i < L + 1.0; i += 1.0) { int idx = (int) (i + L); g[idx] = tmpG.get(idx, 0)[0]; // from http://www.cedar.buffalo.edu/~srihari/CSE555/Normal2.pdf dg[idx] = -i * g[idx] / sigma_sq; d2g[idx] = (-sigma_sq + i * i) * g[idx] / sigma_quad; } } /* 1st and 2nd derivative of smoothed curve point */ void getdX(double[] x, int n, double sigma, boolean isOpen) { int L = (g.length - 1) / 2; gx1 = dgx1 = d2gx1 = 0.0; for (int k = -L; k < L + 1; k++) { double x_n_k; if (n - k < 0) { if (isOpen) { //open curve - mirror values on border x_n_k = x[-(n - k)]; } else { //closed curve - take values from end of curve x_n_k = x[x.length + (n - k)]; } } else if (n - k > x.length - 1) { if (isOpen) { //mirror value on border x_n_k = x[n + k]; } else { x_n_k = x[(n - k) - x.length]; } } else { x_n_k = x[n - k]; } gx1 += x_n_k * g[k + L]; //gaussians go [0 -> M-1] dgx1 += x_n_k * dg[k + L]; d2gx1 += x_n_k * d2g[k + L]; } } /* 0th, 1st and 2nd derivatives of whole smoothed curve */ void getdXcurve(double[] x, double sigma, boolean isOpen) { gx = new double[x.length]; dx = new double[x.length]; d2x = new double[x.length]; for (int i = 0; i < x.length; i++) { getdX(x, i, sigma, isOpen); gx[i] = gx1; dx[i] = dgx1; d2x[i] = d2gx1; } } /* compute curvature of curve after gaussian smoothing from "Shape similarity retrieval under affine transforms", Mokhtarian & Abbasi 2002 curvex - x position of points curvey - y position of points kappa - curvature coeff for each point sigma - gaussian sigma */ void computeCurveCSS(double[] curvex, double[] curvey, double sigma, boolean isOpen) { int M = (int) Math.round((10.0 * sigma + 1.0) / 2.0) * 2 - 1; assert (M % 2 == 1); //M is an odd number getGaussianDerivs(sigma, M);//, g, dg, d2g double[] X, XX, Y, YY; getdXcurve(curvex, sigma, isOpen); smoothX = gx.clone(); X = dx.clone(); XX = d2x.clone(); getdXcurve(curvey, sigma, isOpen); smoothY = gx.clone(); Y = dx.clone(); YY = d2x.clone(); kappa = new double[curvex.length]; for (int i = 0; i < curvex.length; i++) { // Mokhtarian 02' eqn (4) kappa[i] = (X[i] * YY[i] - XX[i] * Y[i]) / Math.pow(X[i] * X[i] + Y[i] * Y[i], 1.5); } } /* find zero crossings on curvature */ ArrayList<Integer> findCSSInterestPoints() { assert (kappa != null); ArrayList<Integer> crossings = new ArrayList<>(); for (int i = 0; i < kappa.length - 1; i++) { if ((kappa[i] < 0.0 && kappa[i + 1] > 0.0) || kappa[i] > 0.0 && kappa[i + 1] < 0.0) { crossings.add(i); } } return crossings; } public void polyLineSplit(MatOfPoint pl) { contourX = new double[pl.height()]; contourY = new double[pl.height()]; for (int j = 0; j < contourX.length; j++) { contourX[j] = pl.get(j, 0)[0]; contourY[j] = pl.get(j, 0)[1]; } } public MatOfPoint polyLineMerge(double[] xContour, double[] yContour) { assert (xContour.length == yContour.length); MatOfPoint pl = new MatOfPoint(); List<Point> list = new ArrayList<>(); for (int j = 0; j < xContour.length; j++) list.add(new Point(xContour[j], yContour[j])); pl.fromList(list); return pl; } MatOfPoint smoothCurve(MatOfPoint curve, double sigma) { int M = (int) Math.round((10.0 * sigma + 1.0) / 2.0) * 2 - 1; assert (M % 2 == 1); //M is an odd number //create kernels getGaussianDerivs(sigma, M); polyLineSplit(curve); getdXcurve(contourX, sigma, false); smoothX = gx.clone(); getdXcurve(contourY, sigma, false); smoothY = gx; Logger.getRootLogger().info("Smooth curve len: " + smoothX.length); return polyLineMerge(smoothX, smoothY); } }